servlet监听器的学习使用(三)
本文为大家分享了servlet监听器的具体学习内容,供大家参考,具体内容如下
1.什么是servlet监听器?
servlet监听器也叫web监听器。是servlet中一种特殊类。能够帮助开发者监听web应用中的特定事件。比如servletcontext,servletsession,servletrequest的创建和销毁,变量的创建和销毁等。
2 监听器常用的用途
通常使用web监听器做以下的内容:
统计在线人数,利用httpsessionlisener
加载初始化信息:利用servletcontextlistener
统计网站访问量
实现访问监控
3.监听器的分类
根据servlet对象可以将监听器划分为三种:servletcontext,servletsession,servletrequest。
4监听器的应用
4.1 servletcontex :对应监控application内置对象的创建和销毁。
当web容器开启时,执行contextinitialized方法;当容器关闭或重启时,执行contextdestroyed方法。
实现方式:直接实现servletcontextlistener接口
package com.learn; import javax.servlet.servletcontext; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; import java.io.*; /** * created by administrator on 2017/09/23. */ public class myservletcontextlistener implements servletcontextlistener { @override public void contextinitialized(servletcontextevent sce) { system.out.println("webapp initital......."); servletcontext servletcontext = sce.getservletcontext(); inputstreamreader inputstreamreader = new inputstreamreader(servletcontext.getresourceasstream("/count/count.txt")); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); try { int icount = integer.parseint(bufferedreader.readline()); icount++; servletcontext.setattribute("count",icount); system.out.println("webapp initital success"); } catch (ioexception e) { e.printstacktrace(); } } @override public void contextdestroyed(servletcontextevent sce) { system.out.println("webapp destroyed ........"); servletcontext servletcontext = sce.getservletcontext(); integer count = (integer) servletcontext.getattribute("count"); if(count != null){ count ++; string filepath = servletcontext.getrealpath("/count"); filepath = filepath+"/count.txt"; try { printwriter printwriter = new printwriter(filepath); printwriter.write(count); printwriter.close(); system.out.println("webapp desdroyed success "); } catch (filenotfoundexception e) { e.printstacktrace(); } } } }
4.2httpsession监控:对应监控session内置对象的创建和销毁。
当打开一个新的页面时,开启一个session会话,执行sessioncreated方法;当页面关闭session过期时,或者容器关闭销毁时,执行sessiondestroyed方法。
实现方式:直接实现httpsessionlistener接口:
package com.learn; import javax.servlet.http.httpsession; import javax.servlet.http.httpsessionevent; import javax.servlet.http.httpsessionlistener; /** * created by administrator on 2017/09/23. */ public class mysessionlistener implements httpsessionlistener{ @override public void sessioncreated(httpsessionevent se) { httpsession session = se.getsession(); system.out.println("新建session,sessionid:"+session.getid()); } @override public void sessiondestroyed(httpsessionevent se) { httpsession session = se.getsession(); system.out.println("销毁session,sessionid:"+session.getid()); } }
4.3servletrequest监控:对应监控request内置对象的创建和销毁。
当访问某个页面时,出发一个request请求,执行requestinitialized方法;当页面关闭时,执行requestdestroyed方法。
实现方式,直接实现servletrequestlistener接口:
package com.learn; import javax.servlet.servletrequestevent; import javax.servlet.servletrequestlistener; import javax.servlet.http.httpservletrequest; /** * created by administrator on 2017/09/23. */ public class myrequestlistener implements servletrequestlistener { @override public void requestdestroyed(servletrequestevent sre) { httpservletrequest request = (httpservletrequest) sre.getservletrequest(); long date = system.currenttimemillis()-(long) (request.getattribute("datecreated")); system.out.println("消耗时间:"+date); } @override public void requestinitialized(servletrequestevent sre) { httpservletrequest request = (httpservletrequest) sre.getservletrequest(); string uri = request.getrequesturi(); uri = request.getquerystring() == null ? uri : (uri + "?" + request.getquerystring()); request.setattribute("datecreated", system.currenttimemillis()); system.out.println("ip:"+request.getremoteaddr()+"uri:"+uri); } }
5监听器web.xml的配置
<listener> <listener-class>com.learn.myservletcontextlistener</listener-class> </listener> <listener> <listener-class>com.learn.mysessionlistener</listener-class> </listener> <listener> <listener-class>com.learn.myrequestlistener</listener-class> </listener>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。