随笔-124  评论-49  文章-56  trackbacks-0

servlet监听器
在WEB应用中响应特定对象的特定事件
好处:更方便的控制application,session,request对象的发生的特定事件
     可以集中处理特定的事件
-------------------------------------------------------------------
HttpSession监听器接口
1 HttpSessionListener(需要配web.xml)
2 HttpSessionAttributeListener(需要配web.xml)
3 HttpSessionBindingListener
4 HttpSessionActivationListener(需要配web.xml)
-------------------------------------------------------------------
HttpSessionListener
1 sessionCreated()//当session对象产生的时候调用的方法
2 sessionDestroyed()//当session对象消灭的时候调用的方法
web.xml配置
<listener>
 <listener-class>webbook.chapter14.CounterListener(对应的实现类)
 </listener-class>
</listener>
---------------------------------------------------------------------
HttpSessionAttributeListener
1 attributeAdded()//添加时调用
2 attributeReplaced()//替换时调用
3 attributeRemoved()//删除时调用
产生事件的代码
session.setAttribute("username","wangwu");
session.setAttribute("username","lisi");
session.removeAttribute("username");
-----------------------------------------------------------------
HttpSessionBindingListener
1 valueBound();
2 vaoueUnbound();
调用机制
如果一个类实现了HttpSessionBindingListener接口,则当这个类的对象通过session.setAttribute()被绑定到Session对象中时,
则该对象的valueBound()方法被自动调用,当这个对象从Session中删除时(调用session.invalidate()和session.removeAtrribute()
方法,或Session对象过期)valueUnbound()方法将被自动调用.
--------------------------------------------------------------------------------------------------------
HttpSessionActivationListener
1 sessionDidActivate();
2 sessionWillPassivate();
---------------------------------------------------------------------------------
例子:
1 建立CounterListener处理类
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class CounterListener implements HttpSessionListener {

 private static long onlineNumber=0;
 public void sessionCreated(HttpSessionEvent se) {
  onlineNumber++;
 }

 public void sessionDestroyed(HttpSessionEvent se) {
  onlineNumber--;
 }
 
 public static long getOnlineNumber(){
  return onlineNumber;
 }
}
2 配置web.xml
<listener>
    <listener-class>com.tags.CounterListener</listener-class>
</listener>
3 JSP页面
<%@page import="com.tags.CounterListener;"%>
<body>
    当前的在线人数:<%=CounterListener.getOnlineNumber() %> <br>
</body>
----------------------------------------------------------------------------
ServletContext监听器接口
ServletContextListener
1 contextInitialized()
2 contextDestroyed()
调用机制
当在web应用中部署了实现该接口的实现类后,在WEB容器加载WEB应用时(如:启动服务器)就会自动调用contextInitialized()方法,
而当WEB容器销毁WEB应用时(如:关闭服务器),会自动调用contextDestroyed()方法.
-----------------------------------------------------------------------------------
ServletContextAttributeListener
1 attributeAdded()
2 attributeReplaced()
3 attributeRemoved()
触发事件的代码
context.setAttribute("counter",new Integer(0));
context.setAttribute("counter",new Integer(100));
context.removeAttribute("counter");
-----------------------------------------------------------
HttpServletRequest监听器
ServletRequestListener
1 requestInitialized();
2 requestDestroyed()
调用机制
与ServletContextListener接口类似,在web应用中部署实现该接口的实现类扣,在HttpServletRequest对象(JSP中的request对象)建立
或者被销毁时,由web容器自动调用requestInitialized()或者requestDestroyed()方法.
--------------------------------------------------------------------------------------
ServletRequestAttributeListener
1 attributeAdded();
2 attributeReplaced();
3 attributeRemoved();
触发事件的代码
request.setAttribute("username","lisi");
request.setAttribute("username","wangwu");
request.removeAttribute("username");

******************************************************************
public class ListenerOnline implements HttpSessionLinstener,ServletContext{
  private  application;
  public void sessionCreated(HttpSessionEvent event){
    Integer n=(Integer)application.getAttrabute("z",1000);
    application.setAttrabute("z",n.IntValue()+1);
   
  }
   public void sessionDestroyed(HttpSessionEvent event){
  }
  public void contextInitialized(ServletContextEvent event){
    application=event.getServletContext();
    application.setAttrabute("z",1000);
  }
   public void contextDestroyed(ServletContextEvent event){
  }
}

<listener>
    <listener-class>com.tags.ListenerOnline</listener-class>
</listener>

<meta http-equiv="refresh" content="3"/>

posted on 2009-11-29 22:47 junly 阅读(264) 评论(0)  编辑  收藏 所属分类: jsp/servlet

只有注册用户登录后才能发表评论。


网站导航: