ServletContext 对象
可直接通过 getServletContext() 获取,也可以通过 getServletConfig().geServletContext() 获取。
tomcat在启动时为每个web项目都创建一个 ServletContext 实例,服务器关闭时会销毁。
一个 Web 应用中的所有 Servlet 共享同一个 ServletContext 对象,因此 Servlet 对象之间可以通过 ServletContext 对象来实现通讯。
ServletContext 接口中的部分方法:
public interface ServletContext{
Object getAttribute(String var1);
Enumeration<String> getAttributeNames();
void setAttribute(String var1, Object var2);
void removeAttribute(String var1);
}
多个 Servlet 通过 ServletContext 对象实现数据共享
// Demo1 将数据存到 ServletContext 对象中
public class Demo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data = "Hello";
//获得ServletContext对象
ServletContext context = this.getServletConfig().getServletContext();
//将data存储到ServletContext对象中
context.setAttribute("data", data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
// Demo2 从 ServletContext 对象中获取数据
public class Demo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
//从ServletContext对象中取出数据
String data = (String) context.getAttribute("data");
response.getWriter().print(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获取 Web.xml 文件中的初始化参数
在 web.xml 文件中使用 <context-param> 标签配置 Web 应用的初始化参数。
<context-param>
<param-name>name</param-name>
<param-value>Noteligible</param-value>
</context-param>
通过 ServletContext 对象获取 Web 应用的初始化参数。
public class Demo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
// 获取 Web 应用的初始化参数
String name = context.getInitParameter("name");
response.getWriter().print(name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
用 ServletContext 实现请求转发
访问的是 Demo4,使用 ServletContext 将请求转发给 Demo5,浏览器会显示 Demo5 的内容。
public class tDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
//获取请求转发对象RequestDispatcher
RequestDispatcher RequestDispatcher = context.getRequestDispatcher("Demo5");
//调用forward方法实现请求转发
RequestDispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write("hello".getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在客户端缓存Servlet的输出
对于不经常变化的数据,在servlet中可以为其设置合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能。例如:
public class ServletDemo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data = "Hello World";
// 设置数据合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能,这里将其设置为1天
response.setDateHeader("expires",System.currentTimeMillis() + 24 * 3600 * 1000);
response.getOutputStream().write(data.getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}