Java监听器Listener使用说明

时间:2023-03-08 16:13:19

转载:http://blog.****.net/meng2602956882/article/details/13511587

1、什么是Java监听器

监听器也叫Listener,是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。

2、Listener接口分类

1.1> ServletContextListener监听ServletContext对象

1.2> ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改

2.1> HttpSessionListener监听Session对象

2.2> HttpSessionActivationListener监听HTTP会话的active和passivate情况,passivate是指非活动的session被写入持久设备(比如硬盘),active相反。

2.3> HttpSessionAttributeListener监听Session中的属性操作

3.1> ServletRequestListener监听Request对象

3.2> ServletRequestAttributeListener监听Requset中的属性操作

3、Java代码

1.1> ServletContextListener

contextInitialized(ServletContextEvent) 初始化时调用

contextDestoryed(ServletContextEvent) 销毁时调用,即当服务器重新加载时调用

2.1>HttpSessionListener

sessionCreated(HttpSessionEvent) 初始化时调用

sessionDestoryed(HttpSessionEvent) 销毁时调用,即当用户注销时调用

3.1>ServletRequestListener

requestinitialized(ServletRequestEvent) 对实现客户端的请求进行监听

requestDestoryed(ServletRequestEvent) 对销毁客户端进行监听,即当执行request.removeAttribute("XXX")时调用

4、Demo

步骤一:新建CountListener类

  1. package web;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.http.HttpSession;
  4. import javax.servlet.http.HttpSessionEvent;
  5. import javax.servlet.http.HttpSessionListener;
  6. public class CountListener implements HttpSessionListener {
  7. private int count = 0;
  8. public void sessionCreated(HttpSessionEvent se) {
  9. count++;
  10. HttpSession session = se.getSession();
  11. ServletContext sct = session.getServletContext();
  12. sct.setAttribute("count", count);
  13. }
  14. public void sessionDestroyed(HttpSessionEvent se) {
  15. count--;
  16. HttpSession session = se.getSession();
  17. ServletContext sct = session.getServletContext();
  18. sct.setAttribute("count", count);
  19. }
  20. }

步骤二:配置监听器

  1. <listener>
  2. <listener-class>web.CountListener</listener-class>
  3. </listener>

步骤三:添加index.jsp页面

  1. <%@ page contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <html>
  4. <head>
  5. <title></title>
  6. </head>
  7. <body>
  8. 当前共有<%=application.getAttribute("count").toString()%>人在线<br>
  9. <a href="logout">登出</a>
  10. </body>
  11. </html>

步骤四:添加LogoutServlet类

package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LogoutServlet extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.invalidate();
out.close();
}
}

步骤四:配置LogoutServlet

<servlet-name>LogoutServlet</servlet-name>
<servlet-class>web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>

步骤五:运行查看结果

      Java监听器Listener使用说明

由于Chrome浏览器再打开选项卡也是与第一个窗口共用sessionId,所以使用火狐浏览器来模拟第二个上线的用户

      Java监听器Listener使用说明

回到Chrome浏览器,点击刷新

      Java监听器Listener使用说明

点击火狐浏览器中的登出,然后回到Chrome浏览器刷新页面

补充:ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext-*.xml
</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

  

  第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口

第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->

BeanFactory这样一来spring中的所有bean都由这个类来创建

第三段,讲如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext-*.xml
        </param-value>
    </context-param>

在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入