Servlet第八篇【ServletContext和ServletConfig】

ServletContext

什么是ServletContext?

答: 当Web容器启动时,它会为每个应用程序创建一个对应ServletContext对象,代表当前Web应用,并且它被所有客户端共享。

多个Servlet通过ServletContext对象实现数据共享

通过ServletContext对象来共享数据,但要注意的是,Session只能在一个客户端共享数据,它独占一个客户端。而ServletContext中的数据是可以供所有客户端共享的。

实现Servlet的请求转发

public class RequestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getServletContext().getRequestDispatcher("/request2").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

获取Web应用的全局参数

<?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">

    <context-param>
        <param-name>name</param-name>
        <param-value>zhangsan</param-value>
    </context-param>

    <servlet>
        <servlet-name>request1</servlet-name>
        <servlet-class>RequestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>request1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
public class RequestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getServletContext().getInitParameter("name");
        System.out.println(name);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

利用ServletContext对象读取资源文件(比如properties文件)

读取的是web应用的根目录

public class RequestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InputStream resourceAsStream = this.getServletContext().getResourceAsStream("info.properties");
        Properties properties=new Properties();
        properties.load(resourceAsStream);
        String name = properties.getProperty("name");
        String age = properties.getProperty("age");
        System.out.println(name+"\n"+age);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

ServletConfig

获取初始化参数

<?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">

    <context-param>
        <param-name>name</param-name>
        <param-value>zhangsan</param-value>
    </context-param>

    <servlet>
        <servlet-name>request1</servlet-name>
        <servlet-class>RequestServlet</servlet-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>request1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
public class RequestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String encoding = this.getServletConfig().getInitParameter("encoding");
        System.out.println(encoding);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

参考链接:
https://blog.csdn.net/gavin_john/article/details/51399425

posted @ 2020-09-24 23:58  幻竹  阅读(160)  评论(0)    收藏  举报