天天看點

JavaWeb-04-ServletContext對象JavaWeb-04-ServletContext對象

JavaWeb-04-ServletContext對象

1.概述

web伺服器在啟動的時候,會為每個web程式都建立一個對應的ServletContext對象,

它代表了目前的web應用

2.共享資料

在一個Servlet中儲存的資料在另一個Servlet中可以拿到

JavaWeb-04-ServletContext對象JavaWeb-04-ServletContext對象

HelloServlet.class

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.getInitParameter();  初始化參數
//        this.getServletConfig();  Servlet配置
//        this.getServletContext(); Servlet上下文
        ServletContext ctx = this.getServletContext();

        String username = "void";//資料
        ctx.setAttribute("username",username);//将一個資料儲存在ServletContext中(鍵值對)


        System.out.println("Hello");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

           

GetServlet.class

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取出HelloServlet的資料
        ServletContext ctx = this.getServletContext();
        String username = (String) ctx.getAttribute("username");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字:"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}

           

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.cmy.servlet.HelloServlet</servlet-class>
<!--        <init-param>-->
<!--            <param-name></param-name>-->
<!--            <param-value></param-value>-->
<!--        </init-param>-->
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>getctx</servlet-name>
        <servlet-class>com.cmy.servlet.GetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getctx</servlet-name>
        <url-pattern>/getctx</url-pattern>
    </servlet-mapping>
</web-app>
           

3.擷取初始化參數

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServletInitP extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = getServletContext();
        String url = ctx.getInitParameter("url");
        resp.getWriter().println(url+"OK");
        System.out.println("hello");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
           
<servlet>
     <servlet-name>getP</servlet-name>
     <servlet-class>com.cmy.servlet.GetServletInitP</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>getP</servlet-name>
    <url-pattern>/getP</url-pattern>
</servlet-mapping>
           

4.請求轉發(getRequestDispatcher)

和重定向的差別

JavaWeb-04-ServletContext對象JavaWeb-04-ServletContext對象

示例

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletReqDispatcher extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = getServletContext();
//        RequestDispatcher requestDispatcher = ctx.getRequestDispatcher("/getP");//轉發的請求路徑
//        requestDispatcher.forward(req,resp);//調用forward方法實作請求轉發
        ctx.getRequestDispatcher("/getP").forward(req,resp);//簡介寫法
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
           
<servlet>
    <servlet-name>grdq</servlet-name>
    <servlet-class>com.cmy.servlet.ServletReqDispatcher</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>grdq</servlet-name>
    <url-pattern>/grdp</url-pattern>
</servlet-mapping>
           

5.讀取資源檔案

Properties

  • 在Java目錄下建立properties(不合約定,需要特殊的設定,見前面文章)
  • 在resource目錄下建立properties

發現:都被打包到了同一個路徑下:classes,稱該路徑為classpath:

讀取properties檔案

db.properties

username=root
password=123456
           
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServletGetRes extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = this.getServletContext();
        InputStream is = ctx.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(is);
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        resp.getWriter().print(username+":"+password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
           

InputStream is = ctx.getResourceAsStream("/WEB-INF/classes/db.properties");

路徑的來源

JavaWeb-04-ServletContext對象JavaWeb-04-ServletContext對象