ServletContext对象
ServletContext对象介绍
ServletContext官方叫Servlet上下文。服务器会为每一个Web应用创建一个ServletContext对象。这个对象全局唯一,而且Web应用中的所有Servlet都共享这个对象。所以叫全局应用程序共享对象。
ServletContext对象的作用
- 相对路径转绝对路径
- 获取容器的附加信息
- 读取配置信息
- 全局容器
相对路径转绝对路径
context.getRealPath("path")
该方法可以将一个相对路径转换为绝对路径,在文件上传与下载时需要用到该方法做路径的转换。
java
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
//转换路径
String realPath = servletContext.getRealPath("image.PNG");
File file=new File(realPath);
获取容器的附加信息
servletContext.getServerInfo()
返回Servlet容器的名称和版本号
servletContext.getMajorVersion()
返回Servlet容器所支持Servlet的主版本号。
servletContext.getMinorVersion()
返回Servlet容器所支持Servlet的副版本号。
java
public class GetBaseInfoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
//获取容器的基本信息
String serverInfo = servletContext.getServerInfo();
//获取容器所支持的主版本号
int majorVersion = servletContext.getMajorVersion();
//获取容器所支持的副版本号
int minorVersion = servletContext.getMinorVersion();
//设置响应编码
resp.setContentType("text/plain;charset=utf-8");
PrintWriter pw = resp.getWriter();
pw.println("容器的基本信息:"+serverInfo);
pw.println("容器所支持Servlet的版本号为:"+majorVersion+"."+minorVersion);
}
}
xml
<servlet>
<servlet-name>getBaseInfoServlet</servlet-name>
<servlet-class>com.java.GetBaseInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getBaseInfoServlet</servlet-name>
<url-pattern>/getBaseInfo.do</url-pattern>
</servlet-mapping>
获取web.xml文件中的信息
xml
<context-param>
<param-name>key</param-name>
<param-value>value</param-value>
</context-param>
servletContext.getInitParameter("key")
该方法可以读取web.xml文件中标签中的配置信息。
servletContext.getInitParameterNames()
该方法可以读取web.xml文件中所有param-name标签中的值。
xml
<context-param>
<param-name>key1</param-name>
<param-value>java1</param-value>
</context-param>
<context-param>
<param-name>key2</param-name>
<param-value>java2</param-value>
</context-param>
java
public class ContextReadInfoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
//获取所有的<param-name>的值
Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
PrintWriter pw = resp.getWriter();
while (initParameterNames.hasMoreElements()){
String name = initParameterNames.nextElement();
String value = servletContext.getInitParameter(name);
pw.println("name:"+name+" = value:"+value);
}
pw.flush();
pw.close();
}
}
xml
<servlet>
<servlet-name>contextReadInfoServlet</servlet-name>
<servlet-class>com.java.ContextReadInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>contextReadInfoServlet</servlet-name>
<url-pattern>/readInfo.do</url-pattern>
</servlet-mapping>
全局容器
servletContext.setAttribute("key",ObjectValue)
向全局容器中存放数据。
servletContext.getAttribute("key")
从全局容器中获取数据。
servletContext.removeAttribute("key")
根据key删除全局容器中的value。
java
public class GlobalContainerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
String s1 = (String) servletContext.getAttribute("key1");
String s2 = (String)servletContext.getAttribute("key2");
PrintWriter pw=resp.getWriter();
pw.println(s1);
pw.println(s2);
pw.flush();
pw.close();
}
@Override
public void init() throws ServletException {
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("key1","java1");
servletContext.setAttribute("key2","java2");
}
}
ServletContext对象生命周期
当容器启动时会创建ServletContext对象并一直缓存该对象,直到容器关闭后该对象生命周期结束。ServletContext对象的生命周期非常长,所以在使用全局容器时不建议存放业务数据。