当前位置:网站首页>JSP中的监听器

JSP中的监听器

2022-06-12 20:24:00 Fairy-KunKun


Servlet中的监听器

需求:mood系统中统计在线人数

监听器属于第三者,不应该影响主体程序,具有可插拔性。

package com.njwbhz.mood.util;
public class OnlineCounter {
  
private static int cnt = 0;
  
public static synchronized void in () {
     
cnt++;
   }
  
public static synchronized void out () {
     
cnt--;
   }
  
  
public static int getOnline () {
     
return cnt;
   }
}

定义一个监听器,专门监听session数据变化。

package com.njwbhz.mood.listener;
import com.njwbhz.mood.util.OnlineCounter;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class OnlineListener implements HttpSessionAttributeListener {
  
@Override
  
public void attributeAdded ( HttpSessionBindingEvent httpSessionBindingEvent ) {
      System.
out.println (
           
"a user is coming"
     
);
      OnlineCounter.in ( );
   }
  
@Override
  
public void attributeRemoved ( HttpSessionBindingEvent httpSessionBindingEvent ) {
      System.
out.println (
           
"a user is outing"
     
);
      OnlineCounter.out ( );
   }
  
@Override
  
public void attributeReplaced ( HttpSessionBindingEvent httpSessionBindingEvent ) {
     
   }
}

配置

<listener >
   <listener-class >com.njwbhz.mood.listener.OnlineListener</listener-class>
</listener>

启动测试

<%@ page import="com.njwbhz.mood.util.OnlineCounter" %>
<%
  
String path = request.getContextPath ( );
   String basePath = request.getScheme ( ) +
"://" + request.getServerName ( ) + ":" + request.getServerPort ( ) + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <base href="<%=basePath%>">
   <title>在线人数</title>
   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
   <meta http-equiv="description" content="this is my page">
   <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
在线人数:<%=OnlineCounter.getOnline ( )%>
</body>
</html>

说明:

监听器为web应用服务的,独立的监听程序,用于监听对象的状态变化。

Servlet中的监听器有很多种,             

规律:监听对象+Listener,实现相应的接口

使用注解做配置

再看一个监听器

ServletContext对象什么时候被创建?

服务器启动时被创建。

Servlet配置了load-on-startupàtomcat启动时,可以将一些准备工作(初始化工作)交给servlet完成。

package com.njwbhz.mood.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet ( value = "/startup", loadOnStartup = 1 )
public class StartServlet extends HttpServlet {
  
@Override
  
public void init ( ) throws ServletException {
      System.
out.println ( "StartServlet execute init" );
   }
}

package com.njwbhz.mood.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
  
@Override
  
public void contextInitialized ( ServletContextEvent servletContextEvent ) {
      System.
out.println (
           
"MyServletContextListener execute init"
     
);
   }
  
@Override
  
public void contextDestroyed ( ServletContextEvent servletContextEvent ) {
   }
}

启动服务器

说明:在web程序中,要为程序做初始化工作,三种解决方案:

(1)监听器

(2)过滤器

(3)Servlet

原网站

版权声明
本文为[Fairy-KunKun]所创,转载请带上原文链接,感谢
https://blog.csdn.net/FairyKunKun/article/details/125244092