ServletContext

1、什么是ServletContext?

  • 运行在JVM上的每一个web应用程序都有一个与之对应的Servlet上下文(Servlet运行环境)
  • Servlet API提供ServletContext接口来表示Servlet上下文,ServletContext对象可以被web应用程序中的所有Servlet访问
  • ServletContext对象是web服务器中的一个已知路径的根

2、原理
ServletContext对象由服务器创建,一个项目只有一个ServletContext对象。在项目任何位置获取到的都是同一个对象,那么不同用户发起的请求获取到的也就是同一个ServletContext对象了,该对象由用户共同拥有。
3、作用
解决不同用户之间数据共享问题
4、特点

  • 由服务器创建
  • 所有用户共享同一个ServletContext对象
  • 所有的Servlet都可以访问到ServletContext中的属性
  • 每一个web项目对应的是一个ServletContext

5、 用法
web.xml文件部分属性

<context-param>
    <param-name>country</param-name>
    <param-value>China</param-value>
</context-param>
<context-param>
    <param-name>city</param-name>
    <param-value>BeiJing</param-value>
</context-param>
<servlet>
    <servlet-name>ContextServlet</servlet-name>
    <servlet-class>com.syf.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ContextServlet</servlet-name>
    <url-pattern>/cs</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>ConfigServlet</servlet-name>
    <servlet-class>com.syf.ConfigServlet</servlet-class>
    <init-param>
        <param-name>Job</param-name>
        <param-value>Program</param-value>
    </init-param>
    <init-param>
        <param-name>Location</param-name>
        <param-value>East</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>ConfigServlet</servlet-name>
    <url-pattern>/config</url-pattern>
</servlet-mapping>
public class ContextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //3种方式获取ServletContext对象
        ServletContext sc1 = this.getServletContext();//常用
        ServletContext sc2 = this.getServletConfig().getServletContext();
        ServletContext sc3 = request.getSession().getServletContext();
        //设置属性
        sc1.setAttribute("name","zhangsan");
        
        //获取web.xml中设置的参数值
        String city = sc2.getInitParameter("city");
        System.out.println(city);//BeiJing
        String country = sc3.getInitParameter("country");
        System.out.println(country);//China
        
        //获取某个文件的绝对路径
        String realPath = sc1.getRealPath("web.xml");
        System.out.println(realPath);
        
        //获取web项目的上下文路径(Tomcat的虚拟目录路径)
        String contextPath = sc1.getContextPath();
        System.out.println(contextPath);
    }
}
public class ContextServlet2 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext sc = this.getServletContext();
        //获取在contextServlet类中设置的属性
        String name = (String) sc.getAttribute("name");
        System.out.println(name);//zhangsan
    }
}

ServletConfig

1、作用
ServletConfig对象是Servlet的专属配置对象,每个Servlet都单独拥有一个ServletConfig对象 ,用来获取web.xml中的配置信息
2、用法

public class ConfigServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        ServletConfig sc = this.getServletConfig();
        
        //获取web.xml中设置的ConfigServlet的配置属性key
        Enumeration<String> keys = sc.getInitParameterNames();
        while (keys.hasMoreElements()){
            String key = keys.nextElement();
            //根据key获取value值
            String value = sc.getInitParameter(key);
            System.out.println(key+"="+value);//Job=Program,Location=East
        }
    }
}

Songyf
4 声望0 粉丝

码帝