学习内容: ServletContext类
1. 什么是 ServletContext?
1、ServletContext 是一个接口,它表示 Servlet 上下文对象
2、一个 web 工程,只有一个 ServletContext 对象实例。
3、ServletContext 对象是一个域对象。
4、ServletContext 是在 web 工程部署启动的时候创建。在 web 工程停止的时候销毁。
什么是域对象?
域对象,是可以像 Map 一样存取数据的对象,叫域对象。
这里的域指的是存取数据的操作范围,整个 web 工程。
存数据 取数据 删除数据
Map put() get() remove()
域对象 setAttribute() getAttribute() removeAttribute();
2. ServletContext 类的四个作用
1、获取 web.xml 中配置的上下文参数 context-param
2、获取当前的工程路径,格式: /工程路径
3、获取工程部署后在服务器硬盘上的绝对路径
web.xml 中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--context-param是上下文参数(它属于整个web工程)-->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<servlet>
<servlet-name>ContextServlet</servlet-name>
<servlet-class>com.Serein.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet</servlet-name>
<url-pattern>/ContextServlet</url-pattern>
</servlet-mapping>
</web-app>
ServletContext 演示代码:
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ContextServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1、获取 web.xml 中配置的上下文参数 context-param
ServletContext context=getServletConfig().getServletContext();
/*
也可以这样写:ServletContext context=getServletContext();
这里的getServletContext()是javax.servlet.GenericServlet中的
代码如下:
public ServletContext getServletContext() {
return this.getServletConfig().getServletContext();
}
*/
String username=context.getInitParameter("username");
System.out.println("username的值是:"+username);
// 2、获取当前的工程路径,格式: /工程路径
System.out.println("当前工程路径:"+context.getContextPath());
// 3、获取工程部署后在服务器硬盘上的绝对路径
// / 斜杠被服务器解析地址为:http://ip:port/工程名/ ; 映射到IDEA代码的web目录
System.out.println("工程部署的路径是:"+context.getRealPath("/"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
4、像 Map 一样存取数据
web.xml 中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>ContextServlet1</servlet-name>
<servlet-class>com.Serein.ContextServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet1</servlet-name>
<url-pattern>/ContextServlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ContextServlet2</servlet-name>
<servlet-class>com.Serein.ContextServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet2</servlet-name>
<url-pattern>/ContextServlet2</url-pattern>
</servlet-mapping>
</web-app>
ServletContext1 演示代码:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ContextServlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取ServletContext对象
ServletContext context = getServletContext();
System.out.println("保存之前: Context1 获取 key1的值是:"+ context.getAttribute("key1"));
//存数据
context.setAttribute("key1", "value1");
//取数据
System.out.println("Context1 中获取域数据key1的值是:"+ context.getAttribute("key1"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
ServletContext 2演示代码:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ContextServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("Context1 中获取域数据key1的值是:"+ context.getAttribute("key1"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
当我们访问了ServletContext1之后会出现一下结果:
紧接着我们访问ServletContext2:
出现这个结果是因为:一个 web 工程,只有一个 ServletContext 对象实例。我们已经在ServletContext1中存入了数据,所以在ServletContext2中也能取到。
但是如果我们在是先访问ServletContext2,因为没有先存入数据,所以取不到数据: