本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:天地无用
日志总数:55
评论数量:43
留言数量:1
访问次数:194855
建立时间:2008年4月17日




[jsp]jsp中session创建或失效时自动触发------谈谈Listener Servlet的应用(转)
软件技术

kkk888929 发表于 2009/6/10 14:30:38

  通过一篇文单我终于明白了,呵呵!接口中的这个监听过程是在session创建或失效时自动触发!     http://tech.ccidnet.com/pub/article/c322_a180671_p1.html import   javax.servlet.http.*;     6 import   javax.servlet.*;     7     8 public   class   OnLineCountListener   implements   HttpSessionListener,     ServletContextListener,ServletContextAttributeListener     9 {     10 private   int   count;     11 private   ServletContext   context   =   null;     12     13 public   OnLineCountListener()     14 {     15 count=0;     16 //setContext();     17 }     18 //创建一个session时激发     19 public   void   sessionCreated(HttpSessionEvent   se)       20 {     21 count++;     22 setContext(se);     23     24 }     25 //当一个session失效时激发     26 public   void   sessionDestroyed(HttpSessionEvent   se)       27 {     28 count--;     29 setContext(se);     30 }     31 //设置context的属性,它将激发attributeReplaced或attributeAdded方法     32 public   void   setContext(HttpSessionEvent   se)     33 {     34 se.getSession().getServletContext().     setAttribute("onLine",new   Integer(count));     35 }     36   //增加一个新的属性时激发     37 public   void   attributeAdded(ServletContextAttributeEvent   event)   {     38     39 log("attributeAdded('"   +   event.getName()   +   "',   '"   +     40         event.getValue()   +   "')");     41     42         }     43             44       //删除一个新的属性时激发     45         public   void   attributeRemoved(ServletContextAttributeEvent   event)   {     46     47 log("attributeRemoved('"   +   event.getName()   +   "',   '"   +     48         event.getValue()   +   "')");     49     50         }     51     52 //属性被替代时激发     53         public   void   attributeReplaced(ServletContextAttributeEvent   event)   {     54     55 log("attributeReplaced('"   +   event.getName()   +   "',   '"   +     56         event.getValue()   +   "')");     57         }     58         //context删除时激发     59           public   void   contextDestroyed(ServletContextEvent   event)   {     60     61 log("contextDestroyed()");     62 this.context   =   null;     63     64         }     65     66         //context初始化时激发     67         public   void   contextInitialized(ServletContextEvent   event)   {     68     69 this.context   =   event.getServletContext();     70 log("contextInitialized()");     71     72         }     73         private   void   log(String   message)   {     74     75         System.out.println("ContextListener:   "   +   message);     76         }           77 }                     <font   color=red><%=getServletContext().getAttribute("onLine")%></font><br>     为何onLine的值一直增加,当一个用户关闭浏览器时,onLine的值也不减1?     被转文件发布时间:2004.11.23 11:26 Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:     ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。     ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。     HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。     HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。     下面我们开发一个具体的例子,这个监听器能够统计在线的人数。在ServletContext初始化和销毁时,在服务器控制台打印对应的信息。当ServletContext里的属性增加、改变、删除时,在服务器控制台打印对应的信息。     要获得以上的功能,监听器必须实现以下3个接口:     HttpSessionListener     ServletContextListener     ServletContextAttributeListener     我们看具体的代码,见示例14-9。     【程序源代码】 1 // ==================== Program Discription ===================== 2 // 程序名称:示例14-9 : EncodingFilter .java 3 // 程序目的:学习使用监听器 4 // ============================================================== 5 import javax.servlet.http.*; 6 import javax.servlet.*; 7 8 public class OnLineCountListener implements HttpSessionListener, ServletContextListener,ServletContextAttributeListener 9 { 10 private int count; 11 private ServletContext context = null; 12 13 public OnLineCountListener() 14 { 15 count=0; 16 //setContext(); 17 } 18 //创建一个session时激发 19 public void sessionCreated(HttpSessionEvent se) 20 { 21 count++; 22 setContext(se); 23 24 } 25 //当一个session失效时激发 26 public void sessionDestroyed(HttpSessionEvent se) 27 { 28 count--; 29 setContext(se); 30 } 31 //设置context的属性,它将激发attributeReplaced或attributeAdded方法 32 public void setContext(HttpSessionEvent se) 33 { 34 se.getSession().getServletContext(). setAttribute("onLine",new Integer(count)); 35 } 36 //增加一个新的属性时激发 37 public void attributeAdded(ServletContextAttributeEvent event) { 38 39 log("attributeAdded('" + event.getName() + "', '" + 40 event.getValue() + "')"); 41 42 } 43 44 //删除一个新的属性时激发 45 public void attributeRemoved(ServletContextAttributeEvent event) { 46 47 log("attributeRemoved('" + event.getName() + "', '" + 48 event.getValue() + "')"); 49 50 } 51 52 //属性被替代时激发 53 public void attributeReplaced(ServletContextAttributeEvent event) { 54 55 log("attributeReplaced('" + event.getName() + "', '" + 56 event.getValue() + "')"); 57 } 58 //context删除时激发 59 public void contextDestroyed(ServletContextEvent event) { 60 61 log("contextDestroyed()"); 62 this.context = null; 63 64 } 65 66 //context初始化时激发 67 public void contextInitialized(ServletContextEvent event) { 68 69 this.context = event.getServletContext(); 70 log("contextInitialized()"); 71 72 } 73 private void log(String message) { 74 75 System.out.println("ContextListener: " + message); 76 } 77 }     【程序注解】    在OnLineCountListener里,用count代表当前在线的人数,OnLineCountListener将在Web服务器启动时自动执行。当OnLineCountListener构造好后,把count设置为0。每增加一个Session,OnLineCountListener会自动调用sessionCreated(HttpSessionEvent se)方法;每销毁一个Session,OnLineCountListener会自动调用sessionDestroyed(HttpSessionEvent se)方法。当调用sessionCreated(HttpSessionEvent se)方法时,说明又有一个客户在请求,此时使在线的人数(count)加1,并且把count写到ServletContext中。ServletContext的信息是所有客户端共享的,这样,每个客户端都可以读取到当前在线的人数。 为了使监听器生效,需要在web.xml里进行配置,如下所示: <listener> <listener-class>OnLineCountListener</listener-class> </listener> 测试程序: <%@ page contentType="text/html;charset=gb2312" %> 目前在线人数: <font color=red><%=getServletContext().getAttribute("onLine")%></font><br> 退出会话: <form action="exit.jsp" method=post> <input type=submit value="exit"> </form> getServletContext().getAttribute("onLine")获得了count的具体值。客户端调用 <%session.invalidate() ;%>     使Session失效,这样监听器就会使count减1。     【运行程序】    web.xml做好以上的配置,把OnLineCountListener放在WEB-INF/class目录下,启动Web服务器,在浏览器里输入以下URL(根据具体情况不同):http://127.0.0.1:8080/ch14/listener.jsp     浏览器将会打印目前在线人数。在服务器端有以下输出: … ContextListener: contextInitialized() ContextListener: attributeReplaced('org.apache. catalina.WELCOME_FILES', '[Ljava.lang.String;@1d98a') … ContextListener: attributeAdded('onLine', '1') ContextListener: attributeReplaced('onLine', '1') ContextListener: attributeReplaced('onLine', '0') ContextListener: attributeReplaced('onLine', '1') ContextListener: attributeReplaced('onLine', '2') (T111) 本文选自飞思图书《精通Java核心技术》  


阅读全文(3403) | 回复(1) | 编辑 | 精华
 


Steve Jobs plays dress-up
软件技术

air max skyline shoes(游客)发表评论于2010/4/2 22:21:16

To say Apple fans were anxiously anticipating Steve Job's keynote on the opening day of this week's developers conference is probably an understatement. But they weren't just waiting for confirmation of news reports that Apple is moving to Intel processors. They were looking for the answer to another important question: Would Jobs deviate from his trademark jeans and black mock turtleneck?Jobs, did, in fact, shock some attendees by donning a black shirt and black pants, something Joy of Tech cartoonists Nitrozac (aka Liza Schmalcel) and Snaggy (aka Bruce Evans) say could have something to do with their June 1 "Steve Jobs Dress-up Game" comic. The somewhat addicting game allows you to dress Jobs up using his "insanely great magic wardrobe," which ranges in theme from pirate chic to biker geek to hippy freak.Perhaps Jobs did take a hint from the 200-some Steve Jobs Dress-up contests entrants, who clearly long to see a wilder side of Jobs. One of the honorable mentions was a Mohawked, tattooed Jobs wearing nothing but his Skivvies and aniPod. Another contest entry portrayed "What happens when Steve listens to his Michael Jackson iPod Shuffle." Our favorite, however, was the Jobs clad in all-white styled by "the designers of the iPod."The game has "been a phenomenal hit," Evans said, air max skyline shoes noting that it's been g...


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144799616 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号