1.监听器介绍
2.过滤器与监听器的区别
3.监听器要素
.
4.内置对象监听接口
1 package listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 import javax.servlet.ServletRequestEvent; 6 import javax.servlet.ServletRequestListener; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpSession; 9 import javax.servlet.http.HttpSessionEvent; 10 import javax.servlet.http.HttpSessionListener; 11 12 public class WebListener implements ServletContextListener,HttpSessionListener,ServletRequestListener{ 13 14 @Override 15 public void contextInitialized(ServletContextEvent sce) { 16 // TODO Auto-generated method stub 17 System.out.println("ServletContext已初始化"); 18 } 19 @Override 20 public void contextDestroyed(ServletContextEvent sce) { 21 // TODO Auto-generated method stub 22 System.out.println("ServletContext已被销毁"); 23 } 24 25 @Override 26 public void sessionCreated(HttpSessionEvent se) { 27 // TODO Auto-generated method stub 28 HttpSession session=se.getSession(); 29 System.out.println("Session已被创建,SessionId:"+session.getId()); 30 } 31 @Override 32 public void sessionDestroyed(HttpSessionEvent se) { 33 // TODO Auto-generated method stub 34 System.out.println("Session已被销毁"); 35 } 36 37 @Override 38 public void requestInitialized(ServletRequestEvent sre) { 39 // TODO Auto-generated method stub 40 HttpServletRequest request=(HttpServletRequest) sre.getServletRequest(); 41 System.out.println("HttpServletRequest已被创建,URI:"+request.getRequestURI()); 42 } 43 @Override 44 public void requestDestroyed(ServletRequestEvent sre) { 45 // TODO Auto-generated method stub 46 System.out.println("HttpServletRequest已被销毁"); 47 } 48 49 50 51 }
ServletContext对象服务器开启立刻初始化,
ServletRequestEvent、HttpSessionListener被调用了Servlet后创建
5.属性监听接口
1 package listener; 2 3 import javax.servlet.ServletContextAttributeEvent; 4 import javax.servlet.ServletContextAttributeListener; 5 import javax.servlet.ServletContextListener; 6 import javax.servlet.ServletRequestAttributeEvent; 7 import javax.servlet.ServletRequestAttributeListener; 8 import javax.servlet.ServletRequestListener; 9 import javax.servlet.http.HttpSession; 10 import javax.servlet.http.HttpSessionActivationListener; 11 import javax.servlet.http.HttpSessionAttributeListener; 12 import javax.servlet.http.HttpSessionBindingEvent; 13 import javax.servlet.http.HttpSessionEvent; 14 15 public class WebAttributeListener implements ServletContextAttributeListener,HttpSessionAttributeListener,ServletRequestAttributeListener{ 16 17 @Override 18 public void attributeAdded(ServletContextAttributeEvent event) { 19 // TODO Auto-generated method stub 20 System.out.println("ServletContext新增属性:"+event.getName()+"->"+event.getValue()); 21 } 22 @Override 23 public void attributeRemoved(ServletContextAttributeEvent event) { 24 // TODO Auto-generated method stub 25 26 } 27 @Override 28 public void attributeReplaced(ServletContextAttributeEvent event) { 29 // TODO Auto-generated method stub 30 31 } 32 33 @Override 34 public void attributeAdded(HttpSessionBindingEvent event) { 35 // TODO Auto-generated method stub 36 System.out.println("HttpSession新增属性:"+event.getName()+"->"+event.getValue()); 37 } 38 @Override 39 public void attributeRemoved(HttpSessionBindingEvent event) { 40 // TODO Auto-generated method stub 41 42 } 43 @Override 44 public void attributeReplaced(HttpSessionBindingEvent event) { 45 // TODO Auto-generated method stub 46 47 } 48 49 @Override 50 public void attributeAdded(ServletRequestAttributeEvent srae) { 51 // TODO Auto-generated method stub 52 System.out.println("ServletRequest新增属性:"+srae.getName()+"->"+srae.getValue()); 53 } 54 @Override 55 public void attributeRemoved(ServletRequestAttributeEvent srae) { 56 // TODO Auto-generated method stub 57 58 } 59 @Override 60 public void attributeReplaced(ServletRequestAttributeEvent srae) { 61 // TODO Auto-generated method stub 62 63 } 64 65 }
调用服务器后监听
6.小练习
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="js/echarts.min.js"></script> <script type="text/javascript" src="js/jquery-3.4.1.js"></script> </head> <body> <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM --> <div id="main" style=" 600px; height: 400px;"></div> <script type="text/javascript"> function showChart(){ $.ajax({ url:"./rt", type:"get", dataType:"json", success:(json)=>{ // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据 var option = { title: { text: '请求流量分析统计' }, tooltip: {}, legend: { data:['访问量'] }, xAxis: { data:json.timeList }, yAxis: {}, series: [{ name: '访问量', type: 'line', data: json.valueList }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } }) } window.setInterval("showChart()",1000); </script> </body> </html>
1 package total; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletContextListener; 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import com.alibaba.fastjson.JSON; 15 16 /** 17 * Servlet implementation class RequestTotalServlet 18 */ 19 @WebServlet("/rt") 20 public class RequestTotalServlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public RequestTotalServlet() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 36 37 38 // TODO Auto-generated method stub 39 ServletContext context=request.getServletContext(); 40 List<String> timeList=(List)context.getAttribute("timeList"); 41 List<Integer> valueList=(List)context.getAttribute("valueList"); 42 response.setContentType("text/html;charset=UTF-8"); 43 44 Map result=new HashMap(); 45 result.put("timeList", timeList); 46 result.put("valueList", valueList); 47 String json=JSON.toJSONString(result); 48 response.getWriter().println(json); 49 } 50 51 /** 52 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 53 */ 54 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 55 // TODO Auto-generated method stub 56 doGet(request, response); 57 } 58 59 }
1 package total; 2 3 import java.text.SimpleDateFormat; 4 import java.util.ArrayList; 5 import java.util.Date; 6 import java.util.List; 7 8 import javax.servlet.ServletContextEvent; 9 import javax.servlet.ServletContextListener; 10 import javax.servlet.ServletRequestEvent; 11 import javax.servlet.ServletRequestListener; 12 import javax.servlet.http.HttpServletRequest; 13 //@WebListener 14 public class RequestTotalListener implements ServletContextListener, ServletRequestListener { 15 16 public void requestDestroyed(ServletRequestEvent sre) { 17 // TODO Auto-generated method stub 18 19 } 20 21 public void requestInitialized(ServletRequestEvent sre) { 22 23 HttpServletRequest request = (HttpServletRequest)sre.getServletRequest(); 24 String url = request.getRequestURL().toString(); 25 if(url.endsWith("/rt") == true) { 26 return; 27 } 28 29 // TODO Auto-generated method stub 30 //TimeList : 10:02 10:03 10:04 31 //ValueList: 5 7 10 32 List<String> timeList= (List) sre.getServletContext().getAttribute("timeList"); 33 List<Integer> valueList= (List) sre.getServletContext().getAttribute("valueList"); 34 Date date=new Date(); 35 SimpleDateFormat sdf=new SimpleDateFormat("HH:mm"); 36 String time=sdf.format(date); 37 38 if(timeList.indexOf(time) == -1) { 39 timeList.add(time); 40 valueList.add(1); 41 sre.getServletContext().setAttribute("timeList", timeList); 42 sre.getServletContext().setAttribute("valueList", valueList); 43 }else { 44 int index=timeList.indexOf(time); 45 int value=valueList.get(index); 46 valueList.set(index,value+1); 47 sre.getServletContext().setAttribute("valueList", valueList); 48 } 49 } 50 51 /** 52 * @see ServletContextListener#contextDestroyed(ServletContextEvent) 53 */ 54 public void contextDestroyed(ServletContextEvent sce) { 55 // TODO Auto-generated method stub 56 57 } 58 59 /** 60 * @see ServletContextListener#contextInitialized(ServletContextEvent) 61 */ 62 public void contextInitialized(ServletContextEvent sce) { 63 // TODO Auto-generated method stub 64 List timeList=new ArrayList(); 65 List valueList=new ArrayList(); 66 67 sce.getServletContext().setAttribute("timeList", timeList); 68 sce.getServletContext().setAttribute("valueList", valueList); 69 } 70 71 }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>request-total</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>total.RequestTotalListener</listener-class> </listener> </web-app>
需用到阿里echarts.jar