1.概述
Servletcontex上下文对象,当application启动时,Tomcat服务器会根据web.xml以及注解的方式,创建一个唯一的ServletContext对象,包含整个项目中很多核心的资源数据 ,并且是一个整个WEB Application的【公共区域】,所有的Servlet程序和Jsp程序都可以使用ServletContext中存储的数据。当Application项目关闭时,ServletContext对象被销毁。
2.特点
- Servletcontext是整个项目中的一个公共共享资源,所有的Servlet都可以对其进行访问,并且只有一个。
- 他是直接作用于Web Application。
- 服务器启动时就会加载,服务器关闭时销毁。
- ServletContext可以进行数据的共享,一般用来进行Servlet之间的参数的传递。
API
//配置对象可以获取
interface ServletConfig{
//获取上下文对象
ServletContext getServletcontext();
}
//请求对象
interface ServletRequest{
//获取上下文对象
ServletContext getServletcontext();
}
abstract class GenericServlet {
ServletContext getServletContext();
}
interface HttpSession {
ServletContext getServletContext();
}
interface ServletContext {
// 获取盘符中的真实路径
String getRealPath(String path);
// 获取服务器信息
String getServerInfo();
// 获取当前web项目中的根路径
String getContextPath();
// 获取属性键值对
Object getAttribute(String key);
// 设置属性键值对
void setAttribute(String key, Object value);
// 移除属性键值对
void removeAttribute(String key);
}
大牛勿喷,刚学俩月。(合十)