【Java】【web】ServletContext

2017-04-09  本文已影响9人  JerichoPH

ServletContext

  1. 可获取全局配置信息
  2. 可获取当前应用任何位置的资源
  3. 域对象,在当前应用可以使多个servlet共享数据

域对象


public class ServletDemo6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置ServletContext属性
        this.getServletContext().setAttribute("name", "张三");
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}

// 读取设置
public class ServletDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = (String) this.getServletContext().getAttribute("name");
        System.out.println(name);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}

获取全局配置

<!-- 配置文件 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>servletDemo</display-name>
    
    <!-- 全局配置 -->
    <context-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </context-param>
    
    <!-- servletDemo -->
    <servlet>
        <servlet-name>servletDemo</servlet-name>
        <servlet-class>com.demo.ServletDemo</servlet-class>
        <!-- 当服务器启动时就直接实例化,这个数字越大优先级越小,最小写1 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>servletDemo</servlet-name>
        <url-pattern>/demo1</url-pattern>
    </servlet-mapping>
</web-app>
// java
public class ServletDemo8 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String encoding = (String) this.getServletContext().getInitParameter(
                "encoding");
        System.out.println(encoding);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}

请求向下转发

// 请求向下转发
public class ServletDemo9 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("这里是demo9");
        ServletContext app = this.getServletContext();
        // 将请求向下传递
        app.getRequestDispatcher("/demo10").forward(request, response);
        
        System.out.println("向下传递结束");
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}

// 接收请求转发
public class ServletDemo10 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("这里是demo10");
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}
上一篇下一篇

猜你喜欢

热点阅读