什么是servletContext对象
Defines a set of methods that a servlet uses to communicate with its servlet container.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as/catalog
and possibly installed via a.war
file.)
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
TheServletContext
object is contained within theServletConfig
object, which the Web server provides the servlet when the servlet is initialized.
在一个服务器中的一个特定URL名字空间(比如,/myApplication)下的所有Servlet,JSP,JavaBean等Web部件的集合构成了一个Web的应用,每一个Web应用(同一JVM),容器都会有一个背景对象,而javax.servlet.ServletContext接口就提供了访问这个背景对象的途径。
每个应用都会有一个ServletContext对象与之关联,当容器分布在多个虚拟机上时,web应用在所分布的每个虚拟机上都拥有一个ServletContext实例。缺省情况下,ServletContext不是分布式的,并且只存在于一个虚拟机上。
ServletContext对象是Web服务器中的一个已知路径的根,Servlet上下文被定位于http://localhost:8080/项目名
获得Servlet对象的方式
- 通过servletConfig获取
@Override
public void init(ServletConfig servletConfig) throws ServletException {
ServletContext servletContext = servletConfig.getServletContext();
}
- 直接通过this获得,由继承HttpServlet继承的GenericServlet类中提供的getServletContext()方法获得
public class QuicktServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得ServletContext对象
ServletContext servletContext = this.getServletContext();
}
}
ServletContext的作用
一. 获得web应用的全局初始化参数
1.在web.xml中配置初始化参数
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
2.获取初始化参数
//获得ServletContext对象
ServletContext servletContext = this.getServletContext();
//获得初始化参数
String driver = servletContext.getInitParameter("driver");
二.在Web应用范围内存取共享数据
注:web应用范围具有以下两层含义:
(1) 表示有web应用的生命周期构成的时间段.
(2) 表示在web应用的生命周期内所有web组件的集合。
setAtrribute(String name,Object obj):把一个java 对象和一个属性名绑定,并存放到ServletContext 中,参数name 指定属性名,参数Object 表示共享数据。
getAttribute(String name):根据参数给定的属性名,返回一个Object类型的对象。
getAttributeNames():返回一个Enumeration 对象,该对象包含了所有存放在ServletContext 中的属性名
removeAttribute(String name) : 根 据 参 数 指 定 的 属 性 名 , 从servletContext 对象中删除匹配的属性。
getRealPath("相对于该web应用的相对地址"):得到绝对路径
示例:
//设置contest属性
servletContext.setAttribute("personName","Jim");
//获取Context属性
String personName = (String)servletContext.getAttribute("personName");
注意
因为getAttributeNames()返回的是ServletContext 中的所有属性名,所以无法使用以下方法获取所有属性值
Enumeration persons = servletContext.getAttributeNames();
while (persons.hasMoreElements()) {
String name = (String) persons.nextElement();
String p = (String) servletContext.getAttribute(name);
servletContext.removeAttribute(name);
}
打印persons获得如下参数
org.apache.tomcat.InstanceManager
org.apache.catalina.jsp_classpath
javax.websocket.server.ServerContainer
org.apache.tomcat.util.scan.MergedWebXml
javax.servlet.context.tempdir
org.apache.jasper.compiler.ELInterpreter
org.apache.catalina.resources
org.apache.tomcat.JarScanner
org.apache.jasper.runtime.JspApplicationContextImpl
org.apache.jasper.compiler.TldLocationsCache
personName
三. 获得web应用的全局初始化参数
使用ServletContext接口可以直接访问web应用中的静态内容文档结构.包括HTML,GIF和JPEG文件。如以下方法:
.getResource
.getResourceAsStream
这两个方法的参数都是以"/"开头的字符串,表示资源相对于context根的相对路径.文档结构可以存在于服务器文件系统,或是war包中,或是在远程服务器上,抑或其他位置.不可以用来获得动态资源,比如,getResource("/index.jsp"),这个方法将返回该jsp文件的源码,而不是动态页面.可以用"Dispatching Requests"获得动态内容.