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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。