欢迎大家关注本博,同时欢迎大家评论交流,可以给个赞哦!!!
ServletContext是Servlet运行环境上下文,每个Web应用都有自己的一个ServletContext,这个对象是全局唯一的,而且项目内所有Servlet都共享这个对象。需要注意的是,分布式环境下,每个节点都有自己对应的ServletContext。
ServletContext
以下是对ServletContext源码做的注释,通过源码来看,ServletContext可以进行获取应用信息、Servlet版本信息、资源信息、请求转发对象、初始化参数以及属性的操作。
package javax.servlet;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;
/**
* 提供一些方法,供Servlet与Servlet容器进行交互,例如获取文件MIME类型等.
* 每个Web应用程序都有一个Servlet上下文.
*
*/
public interface ServletContext {
/**
* 方法返回web应用的上下文路径,就是我们部署的应用的根目录名称.
*/
public String getContextPath();
/**
* 根据资源定位符,获取其Servlet上下文.
*/
public ServletContext getContext(String uripath);
/**
* 返回Servlet容器支持的Servlet API主版本号.
*/
public int getMajorVersion();
/**
* 返回Servlet容器支持的Servlet API次版本号.
*/
public int getMinorVersion();
/**
* 返回指定文件的MIME类型.
* @param file 文件.
* @return MIME类型.
*/
public String getMimeType(String file);
/**
* 返回一个类似于目录的列表,其中列出了web应用程序中资源的所有路径,这些资源的最长子路径与提供的路径参数匹配.
* 例如输入/pages,会以路径的方式返回pages目录下的所有文件.
* @param path 目录.
* @return 指定目录下的文件列表.
*/
public Set getResourcePaths(String path);
/**
* 根据指定路径返回资源的URL,路径必须以/开头,代表当前Web应用程序的根目录.
* @param path 资源路径.
* @return 代表资源的URL.
* @throws MalformedURLException .
*/
public URL getResource(String path) throws MalformedURLException;
/**
* 使用相对于根目录的路径访问到web目录下的所有文件,而不知道绝对路径.
* @param path 相对路径.
* @return 资源输入流.
*/
public InputStream getResourceAsStream(String path);
/**
* 返回一个RequestDispatcher对象,该对象扮演着一个给定资源包装者的角色.
* 一个RequestDispatcher对象可以用于传送一个请求到特定资源或者把特定资源包含到一个响应当中.
* 该特定资源可以是动态的也可以是静态的.
* @param path 给定资源路径.
* @return RequestDispatcher对象.
*/
public RequestDispatcher getRequestDispatcher(String path);
/**
* 为指定名字的servlet对象返回一个RequestDispatcher对象.
* 一个Servlet实例的名称可以由ServletConfig.getServletName()获取.
* @param name Servlet名称.
* @return RequestDispatcher对象.
*/
public RequestDispatcher getNamedDispatcher(String name);
/**
* @deprecated
* 根据Servlet名称获取Servlet.
*/
public Servlet getServlet(String name) throws ServletException;
/**
* @deprecated
* 获取所有Servlet的枚举.
*/
public Enumeration getServlets();
/**
* @deprecated
* 获取所有Servlet名称的枚举.
*/
public Enumeration getServletNames();
/**
* 把指定的信息写进servlet日志文件,通常是事物日志.日志文件的名称和类型与servlet容器有关.
*/
public void log(String msg);
/**
* @deprecated
* 把指定的信息写进servlet日志文件,通常是事物日志.日志文件的名称和类型与servlet容器有关.
*/
public void log(Exception exception, String msg);
/**
* 把指定的信息写进servlet日志文件,通常是事物日志.日志文件的名称和类型与servlet容器有关.
*/
public void log(String message, Throwable throwable);
/**
* 根据相对路径获取文件路径.
* "" -> C:\项目根.
* "/" -> C:\项目根\.
* "/pp" -> C:\项目根\pp
* @param path 相对路径.
* @return 文件绝对路径.
*/
public String getRealPath(String path);
/**
* 获取Servlet容器版本号.
* @return Servlet容器版本号.
*/
public String getServerInfo();
/**
* 根据指定名称获取初始化参数值.
* @param name 参数名称.
* @return 参数值.
*/
public String getInitParameter(String name);
/**
* 获取初始化参数枚举.
* @return 参数枚举.
*/
public Enumeration getInitParameterNames();
/**
* 根据给定属性名,从ServletContext中获取属性.
* @param name 属性名.
* @return 属性值.
*/
public Object getAttribute(String name);
/**
* 获取ServletContext属性名枚举.
* @return 属性名枚举.
*/
public Enumeration getAttributeNames();
/**
* 设置ServletContext属性.
*/
public void setAttribute(String name, Object object);
/**
* 移除ServletContext属性.移除后,获取此属性返回null.
* @param name
*/
public void removeAttribute(String name);
/**
* 返回ServletContext对应应用的名称.
* 该名称配置于web.xml的<display-name>标签中.
* @return 应用名称.
*/
public String getServletContextName();
}
ServletContextListener
ServletContextListener主要负责监听ServletContext的初始化和销毁,实现ServletContextListener可以接收此事件。
package javax.servlet;
import java.util.EventListener;
/**
* ServletContext变更事件监听.
*/
public interface ServletContextListener extends EventListener {
/**
* ServletContext初始化事件.
* Servlet容器启动时调用,此方法执行之后,Servlet容器继续对Filter初始化.
* @param sce .
*/
public void contextInitialized(ServletContextEvent sce);
/**
* ServletContext销毁事件.
* Servlet容器停止时调用,此方法执行之前,会先销毁所有Servlet和Filter过滤器.
* @param sce .
*/
public void contextDestroyed(ServletContextEvent sce);
}
ServletContextEvent
ServletContextEvent是为ServletContextListener提供的事件对象,主要是继承EventObject,同时保存了ServletContext实例。
package javax.servlet;
/**
* ServletContext更改事件.
*/
public class ServletContextEvent extends java.util.EventObject {
/**
* Construct.
*/
public ServletContextEvent(ServletContext source) {
super(source);
}
/**
* 更改的ServletContext.
* @return ServletContext 实例.
*/
public ServletContext getServletContext() {
return (ServletContext) super.getSource();
}
}
ServletContextAttributeListener
ServletContextAttributeListener主要负责监听ServletContext的属性变化,包括属性的增加、删除和修改,实现ServletContextAttributeListener可以接收属性变化事件。
package javax.servlet;
import java.util.EventListener;
/**
* ServletContext 属性事件监听.
*/
public interface ServletContextAttributeListener extends EventListener {
/**
* ServletContext新增属性事件.在属性新增后调用.
* @param scab 事件.
*/
public void attributeAdded(ServletContextAttributeEvent scab);
/**
* ServletContext移除属性事件.在属性移除后调用.
* @param scab 事件.
*/
public void attributeRemoved(ServletContextAttributeEvent scab);
/**
* ServletContext替换属性(即修改)事件.在属性替换后调用.
* @param scab 事件.
*/
public void attributeReplaced(ServletContextAttributeEvent scab);
}
ServletContextAttributeEvent
ServletContextAttributeEvent是为ServletContextAttributeListener提供的事件对象,主要继承了ServletContextEvent,即ServletContextListener的事件对象,其中保存了ServletContext对象以及修改的属性名和属性值。
package javax.servlet;
/**
* ServletContext 属性事件.
*/
public class ServletContextAttributeEvent extends ServletContextEvent {
/**
* 属性名.
*/
private String name;
/**
* 属性值.
*/
private Object value;
/**
* Constructor.
* @param source 受影响的ServletContext.
* @param name 属性名.
* @param value 属性值.
*/
public ServletContextAttributeEvent(ServletContext source, String name, Object value) {
super(source);
this.name = name;
this.value = value;
}
/**
* 获取属性名.
* @return 属性名.
*/
public String getName() {
return this.name;
}
/**
* 获取属性值.
* @return 属性值.
*/
public Object getValue() {
return this.value;
}
}
<context-param> 配置
<context-param>
<description></description>
<param-name></param-name>
<param-value></param-value>
</context-param>
<context-param>详解
<context-param>标签很简单,包含三个子标签<description>、<param-name>、<param-value>。<description>用于描述配置的信息,<param-name>用于自定参数名,<param-value>用于指定参数值。同时<context-param>可以存在0或多个,分别配置不同的参数。
总结
ServletContext就像是全局的管理器,保持着全局的信息。接触过JSP的同学可能还依稀记得JSP的9个内置对象,其中有一个叫作Application的对象,是作为全局引用使用的,其实Application就是ServletContext的一个实例。
更好的了解ServletContext,可以帮助我们更好的理解Web的设计方式,同时在需要解决框架自身问题的时候,可能会派上很大的用场。
若文中存在错误和不足,欢迎指正!
本博微信公众号“超哥说码”,欢迎大家订阅,公众号正在完善中,会及时将更优质的博文推送于您!