1. What is a servlet listener?
Serlet listeners are also called web listeners. Is a special class in servlet s. It can help developers monitor specific events in Web applications. For example, the creation and destruction of ServletContext, ServletSession, ServletRequest, variable creation and destruction, etc.
2 Common uses of monitors
Web listeners are usually used to do the following:
Statistics of online population, using HttpSession Lisener
Load initialization information: using ServletContextListener
Statistical website visits
Implementing access monitoring
3. Classification of listeners
According to the servlet object, listeners can be divided into three types: ServletContext, ServletSession and ServletRequest.
Application of 4 Monitors
4.1 ServletContex: Corresponding to the creation and destruction of built-in objects for monitoring application s.
When the web container is open, the contextInitialized method is executed; when the container is closed or restarted, the contextDestroyed method is executed.
Implementation: Direct implementation of ServletContextListener interface
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 monitoring: Creation and destruction of built-in objects corresponding to monitoring session s.
When a new page is opened, a session session session session is opened and the session Created method is executed; when the page closes the session expires, or when the container closes and destroys, the session Destroyed method is executed.
Implementation: Direct implementation of HttpSessionListener interface:
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("Newly build session,sessionId: "+session.getId()); } @Override public void sessionDestroyed(HttpSessionEvent se) { HttpSession session = se.getSession(); System.out.println("Destruction session,sessionId: "+session.getId()); } }
4.3 ServletRequest monitoring: Creation and destruction of built-in objects corresponding to monitoring request s.
When a page is accessed, a request request request is made to execute the request Initialized method; when the page is closed, the request Destroyed method is executed.
Implementing the ServletRequestListener interface directly:
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("Time consumed:"+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); } }
Configuration of 5 listener 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>