WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
ServletContext的应用:
1.做为域对象可以在整个web应用范围内共享数据。
这里涉及到一些概念:
域对象:在一个可以被看见的范围内共享数据用到对象
作用范围:整个web应用范围内共享数据
生命周期:当服务器启动web应用加载后创建出ServletContext对象后,域产生。当web应用被移除出容器或服务器关闭,随着web应用的销毁域销毁。
常用方法:
void setAttribute(String name,Object object) :在这个ServletContext中绑定对象到给定的属性名。
void removeAttribute(String name) :从这个ServletContext中移除给定名称的属性。
Object getAttribute(String name):返回具有给定名称的servlet容器属性,如果没有该名称的属性,则返回null。
代码示例:
package com.alin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletContext")
public class ServletContext extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
javax.servlet.ServletContext context = this.getServletContext();
context.setAttribute("name", "bluehonour");
String value = (String)context.getAttribute("name");
System.out.println("name --> " + value);
context.removeAttribute("name");
value = (String)context.getAttribute("name");
System.out.println("name --> " + value);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2、获取WEB应用的初始化参数
我们在第一段中,通过< init-param>标签为某一个单独的servlet加配置信息,这种配置信息在其他的Servlet中是无法访问到的。可如果我们使用< context-param>标签(与Servlet标签并列)为整个Web应用配置属性的话,那所有的Servlet就都能访问里面的参数了。例如:可以把数据库的配置信息放在这里。
这里涉及到一些概念不要混淆:
请求参数 parameter — 浏览器发送过来的请求中的参数信息
初始化参数 initparameter — 在web.xml中为Servlet或ServletContext配置的初始化时带有的基本参数
域属性 attribute — 四大作用域中存取的键值对
代码举例:
在web.xml中为整个web应用添加初始化参数:用户名、密码。代码位置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ServletPractice</display-name>
<context-param>
<param-name>username</param-name>
<param-value>bluehonour</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>www.bluehonour.com</param-value>
</context-param>
String getInitParameter(String name) :返回包含命名上下文范围的初始化参数值的字符串,如果参数不存在,则返回null
Enumeration< String>getInitParameterNames() :将上下文的初始化参数的名称作为字符串对象的枚举类型返回,或者如果上下文没有初始化参数,则返回空枚举。
package com.alin;
import java.io.IOException;
import java.util.Enumeration;
import javax.print.DocFlavor.STRING;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ServletContext")
public class ServletContext extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
javax.servlet.ServletContext context = this.getServletContext();
String value = context.getInitParameter("username");
System.out.println("name: username ---> value: "+ value);
System.out.println("----------------------------");
Enumeration enumeration = context.getInitParameterNames();
while(enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
value = context.getInitParameter(name);
System.out.println("name: " + name + " ---> value: "+ value);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
结果:
name: username ---> value: bluehonour
----------------------------
name: password ---> value: www.bluehonour.com
name: username ---> value: bluehonour