ServletContext 对象

问题:Request 解决了一次请求内的数据共享问题,session 解决了用户不同请求的数据共享问题,那么不同的用户的数据共享该怎么办呢?

解决:使用 ServletContext 对象

作用:解决了不同用户的数据共享问题

原理:ServletContext 对象由服务器进行创建,一个项目只有一个对象。不管在项目的任意位置进行获取得到的都是同一个对象,那么不同用户发起的请求获取到的也就是同一个对象了,该对象由用户共同拥有。

  • 特点
    • 服务器进行创建
    • 用户共享
    • 一个项目只有一个

生命周期:服务器启动到服务器关闭

作用域:项目内

  • 使用
    • 获取ServletContext对象
      • 第一种方式:ServletContext sc=this.getServletContext();
      • 第二种方式:ServletContext sc2=this.getServletConfig().getServletContext();
      • 第三种方式:ServletContext sc3=req.getSession().getServletContext();
    • 使用ServletContext对象完成数据共享
      • 数据存储 sc.setAttribute(String name, Object value);
      • 数据获取 sc.getAttribute(“str”) 返回的是Object类型
      • 注意:不同的用户可以给ServletContext对象进行数据的存取。获取的数据不存在返回null。
    • 获取项目中web.xml文件中的全局配置数据
      • sc.getInitParameter(String name); 根据键的名字返回web.xml中配置的全局数据的值,返回String类型。如果数据不存在返回null。
      • sc.getInitParameterNames();返回键名的枚举
    • 全局配置方式
      • 注意 一组<-context-param>标签只能存储一组键值对数据,多组可以声明多个<-context-param>进行存储。
      • 作用:将静态数据和代码进行解耦。
        1
        2
        3
        4
            <context-param>
        <param-name>name</param-name>
        <param-value>zhangsan</param-value>
        </context-param>
        1
        2
        3
        4
        5
        6
        - 获取项目webroot下的资源的绝对路径。
        - **String path=sc.getRealPath(String path);**
        - **获取的路径为项目根目录,path参数为项目根目录中的路径**
        - 获取webroot下的资源的流对象
        - InputStream is = sc.getResourceAsStream(String path);
        - **注意:此种方式只能获取项目根目录下的资源流对象,class文件的流对象需要使用类加载器获取。path参数为项目根目录中的路径**
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
public class ServletContextServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContex对象
//第一种方式
ServletContext sc = this.getServletContext();
//第二种方式
ServletContext sc2 = this.getServletConfig().getServletContext();
//第三种方式
ServletContext sc3 = req.getSession().getServletContext();

//使用ServletContext对象完成数据共享
sc.setAttribute("str", "ServletContext对象学习");//数据存储

//获取项目web.xml的全局配置数据
String str = sc.getInitParameter("name");
System.out.println("全局配置"+str);
System.out.println("全局配置参数:"+str);
//获取项目根目录下的资源的绝对路径
//String path="D:\\apache-tomcat-7.0.56\\webapps\\sc\\doc\\1.txt";
String path=sc.getRealPath("/doc/1.txt");
System.out.println(path);
//获取项目根目录下资源的流对象
InputStream is = sc.getResourceAsStream("/doc/1.txt");

}
}
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
40
<?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">
<!--配置全局数据 -->
<context-param>
<param-name>name</param-name>
<param-value>zhangsan</param-value>
</context-param>

<display-name>06-ServletContext</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletContextServlet</servlet-name>
<servlet-class>com.kylin.servlet.ServletContextServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletContextServlet2</servlet-name>
<servlet-class>com.kylin.servlet.ServletContextServlet2</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>ServletContextServlet</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletContextServlet2</servlet-name>
<url-pattern>/context2</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

ServletConfig对象

问题:使用ServletContext对象可以获取web.xml中的全局配置文件,在 web.xml中每个 Servlet 也可以进行单独的配置,那么该怎么获取配置信息呢

解决:使用 ServletConfig 对象

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

  • 使用
    • 获取ServletConfig对象 ServletConfig sc = this.getServletConfig();
    • 获取web.xml中的配置数据 String code = sc.getInitParameter(“config”);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.kylin.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletConfigServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//获取ServletConfig对象
ServletConfig sc = this.getServletConfig();
//获取web.xml中的配置数据
String code = sc.getInitParameter("config");
System.out.println(code);
}
}
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
<?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>07-ServletConfig</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletConfigServlet</servlet-name>
<servlet-class>com.kylin.servlet.ServletConfigServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>utf-8</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>ServletConfigServlet</servlet-name>
<url-pattern>/sg</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>