ServletContext的使用

ServletContext:

ServletContext表示Servlet应用程序。每个Web应用程序只有一个上下文。在将一个应用程序同时部署到多个容器的分布式环境中,每台Java虚拟机上的Web应用都会有一个ServletContext对象。

通过在ServletConfig中调用getServletContext方法,可以获得ServletContext。

有了ServletContext,就可以共享从应用程序中的所有资料处访问到的信息,并且可以动态注册Web对象。前者将对象保存到ServletContext中的一个内部的Map中。保存在ServletContext中的对象被称作属性。

1:实现数据共享 setAttribute(key,value)  getAttribute(key) ,获取域对象

2:获取全局配置信息: getInitParameter() 

3:获得应用下任何资源的路径

例一:通过调用GenericServlet的 getServletContext对象得到ServletContext对象,获取域对象

先运行myservlet1.java,在运行myservlet2.java

myservlet1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class myservlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通过调用GenericServlet的 getServletContext对象得到ServletContext对象
        //先访问demo1,在访问demo2
        ServletContext application=this.getServletContext();
        application.setAttribute("name", "tom");
        System.out.println(application.getClass().getName());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}

myservlet2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class myservlet2 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name=(String) this.getServletContext().getAttribute("name");
         
        if(name==null){
            System.out.println("你不能直接访问这个类");
        }
        System.out.println(name);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}

  运行效果图:

例二:获取全局配置信息

获取encoding的配置信息

myservlet3.java

1
2
3
4
5
6
7
8
9
10
11
public class myservlet3 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String encoding=this.getServletContext().getInitParameter("encoding");
        System.out.println(encoding);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}

 web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <context-param>
  <param-name>encoding</param-name>
  <param-value>utf-8</param-value>
  </context-param>
  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>com.zk.myservlet.myservlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>myservlet2</servlet-name>
    <servlet-class>com.zk.myservlet.myservlet2</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>myservlet3</servlet-name>
    <servlet-class>com.zk.myservlet.myservlet3</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/demo1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>myservlet2</servlet-name>
    <url-pattern>/demo2</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>myservlet3</servlet-name>
    <url-pattern>/demo3</url-pattern>
  </servlet-mapping> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

例三:获取资源路径

使用this.getServletContext().getRealPath获取文件的资源路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class myservlet1 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String apath=this.getServletContext().getRealPath("/WEB-INF/a.properties");//参数一定要以斜杠开头
        System.out.println(apath);
        //创建一个properties
        Properties pro=new Properties();
        pro.load(new FileInputStream(apath));
        System.out.println(pro.getProperty("akey"));
         
        String bpath=this.getServletContext().getRealPath("/WEB-INF/classes/b.properties");
        System.out.println(bpath);
        Properties pro2=new Properties();
        pro2.load(new FileInputStream(bpath));
        System.out.println(pro2.getProperty("key"));
         
        String cpath=this.getServletContext().getRealPath("/WEB-INF/classes/com/zk/myservlet/c.properties");
        System.out.println(cpath);
        Properties pro3=new Properties();
        pro3.load(new FileInputStream(cpath));
        System.out.println(pro3.get("ckey"));
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}

  运行结果图:

 

posted @   leagueandlegends  阅读(202)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示