ServletContext

ServletContext

ServletContext: servlet上下文。

当web容器启动并加载你发布的项目时,它会为你的项目创建一个ServletContext对象,这个对象全局唯一。你的项目中可能有n多个自己写的servlet,而这个ServletContext凌驾于你写的servlet之上。它可以实现不同servlet之间的数据共享。
但在官方API中有如下原话:这个现阶段先不去深究,但要知道有这个概念.
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global).

ServletContext与Servlet的关系大概如下所示:它联系着不同的Servlet,使它们之间能够相互通信,所以叫Servlet上下文。

image-20210112210919945

以下列举一些ServletContext的应用:

共享数据

// 第一个Servlet向上下文中存入username数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //解决中文乱码
        response.setHeader("content-type","text/html;charset=UTF-8");
        /*
            ServletContext对象包含在ServletConfig对象中,可以通过ServletConfig中的
            getServletContext()方法来获取ServletContext对象,
            GenericServlet实现了ServletConfig接口,
            HttpServlet又继承了GenericServlet,
            所以通过getServletContext()可以得到ServletContext对象
         */

        //向ServletContext对象中存入一个username,值为xxgbl。
        getServletContext().setAttribute("username","xxgbl");
        response.getWriter().println("向上下文中存入username");
    }


//第二个Servlet从上下文中取出username数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //从上下文中取出username,测试username的值
        String username = (String) getServletContext().getAttribute("username");
        response.getWriter().println(username);

   }

去浏览器中看结果:

image-20210112213755951

image-20210112213809260

这样就实现了不同Servlet之间的通信,实现了数据共享。

获取初始化参数

web.xml中可以配置初始化参数:

image-20210112215507352

使用ServletContext可以获取这些参数:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //获取初始化参数
    String username1 = getServletContext().getInitParameter("username1");
    String username2 = getServletContext().getInitParameter("username2");

    response.getWriter().println(username1);
    response.getWriter().println(username2);

}

去网页看结果:

image-20210112215546262

请求转发

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //将请求转发给MyFirstServlet
    getServletContext().getRequestDispatcher("/MyFirstServlet").forward(request,response);
}

当请求ThirdServlet时,请求会被转发到MyFirstServlet上

image-20210112220227933

读取资源文件

创建一个properties资源文件:

image-20210112220558894

编写Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //获取配置文件的输入流
    InputStream resource = getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

    //读取文件
    Properties properties = new Properties();
    properties.load(resource);
    String username = properties.getProperty("username");
    String password = properties.getProperty("password");

    response.getWriter().println(username);
    response.getWriter().println(password);
}

去网页看结果:

image-20210112221416709

posted @ 2021-01-12 23:26  xxgbl  阅读(87)  评论(0)    收藏  举报