ServletContextListener

来源:互联网 发布:网络教育本科考试难吗 编辑:程序博客网 时间:2024/06/04 19:57

ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。所以,为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。

换一种方式说吧,运行在JAVA虚拟机中的每一个Web应用程序都有一个与之相关的Servlet上下文。ServletContext对象是Web服务器中的一个已知路径的根,Servlet上下文被定位于http://localhost:8080/项目名.以 /项目名 请求路径(称为上下文路径)开始的所有请求被发送到与此ServletContext关联的Web应用程序。一个ServletContext对象表示了一个Web应用程序的上下文。

Servlet上下文:Servlet上下文提供对应用程序中所有Servlet所共有的各种资源和功能的访问。Servlet上下文API用于设置应用程序中所有Servlet共有的信息。Servlet可能需要共享他们之间的共有信息。运行于同一服务器的Servlet有时会共享资源,如JSP页面、文件和其他Servlet。

举例:

如,做一个购物类的网站,要从数据库中提取物品信息,如果用session保存这些物品信息,每个用户都访问一便数据库,效率就太低了;所以要用来Servlet上下文来保存,在服务器开始时,就访问数据库,将物品信息存入Servlet上下文中,这样,每个用户只用从上下文中读入物品信息就行了。


*************************************************************

作用:监听ServletContext的一些信息

比如:创建,销毁的动作

比如:ServletContext的属性的增加,修改,删除的动作

必须实现的2个接口,ServletContextListener和ServletContextAttributeListener

 

web.xml配置:

<listener>
  <listener-class>listener.StoreServletContextListener</listener-class>
 </listener>

 

************************************************************

 

StoreServletContextListener:

public class StoreServletContextListener implements ServletContextListener,
  ServletContextAttributeListener {

 private ServletContext servletContext;

 // context要删除时调用的方法
 public void contextDestroyed(ServletContextEvent arg0) {
  this.logout("contextDestroyed >>> 销毁");
  this.servletContext = null;
 }

 // context要创建时调用的方法
 public void contextInitialized(ServletContextEvent servletContextEvent) {
  this.servletContext = servletContextEvent.getServletContext();
  this.logout("contextInitialized>>> 创建");
 }

 public void attributeAdded(
   ServletContextAttributeEvent servletContextAttributeEvent) {
  this.logout("attributeAdded >>> 增加:name:"
    + servletContextAttributeEvent.getName() + " value: "
    + servletContextAttributeEvent.getValue());
 }

 public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
  this.logout("attributeRemoved >>> 删除:name:"
    + servletContextAttributeEvent.getName() + " value: "
    + servletContextAttributeEvent.getValue());
 }

 public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
  this.logout("attributeReplaced >>> 修改:name:"
    + servletContextAttributeEvent.getName() + " value: "
    + servletContextAttributeEvent.getValue());
 }

 private void logout(String message) {
  PrintWriter out = null;
  try {
   out = new PrintWriter(
     new FileOutputStream("D://StoreLog.txt", true));
   out.println(new Date().toString()
     + "FROM StoreServletContextListener :" + message);
   out.close();
  } catch (Exception e) {
   out.close();
   e.printStackTrace();
  }
 }

}

 

*******************************************************

 

jsp测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.print("test 开始》》》》》");
out.print("增加属性》》》》》");
getServletContext().setAttribute("Attribute","111");
out.print("修改属性》》》》》");
getServletContext().setAttribute("Attribute","222");
out.print("删除属性》》》》》");
getServletContext().removeAttribute("Attribute");
%>

</body>
</html>

原创粉丝点击