Servlet3.0下监听器的使用
使用Servlet 3.0的前提条件
1)使用Servlet 3.0新标准jar包
2)JDK必须是1.6以上版本
3)编译器的编译级别为6.0
4)在web.xml文件中,使用3.0规范
5)使用支持Servlet 3.0特性的Web容器,比如Tomcat7
Servlet3.0下监听器的用法
@WebListener
该注解用于将类声明为监听器,被@WebListener标注的类必须实现以下至少一个接口:
ServletContextListener
ServletContextAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionListener
HttpSessionAttributeListener
Servlet3.0下无法确定监听器的启动顺序,和过滤器是一样的。
@WebListener的常用属性
web.xml中的配置
<listener>
<listener-class>com.listeber.SimpleListener</listener-class>
</listener>
演变为:
@WebListener("This is only a demo listener")
统计在线用户及人数
实例代码
User.java源代码:
package com.domain; public class User { private String sessionIdString; private String ipString; private String firstTimeString; public String getSessionIdString() { return sessionIdString; } public void setSessionIdString(String sessionIdString) { this.sessionIdString = sessionIdString; } public String getIpString() { return ipString; } public void setIpString(String ipString) { this.ipString = ipString; } public String getFirstTimeString() { return firstTimeString; } public void setFirstTimeString(String firstTimeString) { this.firstTimeString = firstTimeString; } }
SessionListener.java源代码:
package com.listener; import java.util.ArrayList; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import com.domain.User; import com.utils.SessionUtil; /** * Application Lifecycle Listener implementation class SimpleListener * */ @WebListener public class SessionListener implements HttpSessionListener { private int userNumber = 0; /** * Default constructor. */ public SessionListener() { // TODO Auto-generated constructor stub } /** * @see HttpSessionListener#sessionCreated(HttpSessionEvent) */ public void sessionCreated(HttpSessionEvent httpSessionEvent) { userNumber++; httpSessionEvent.getSession().getServletContext().setAttribute("userNumber", userNumber); } /** * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent) */ public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { userNumber--; httpSessionEvent.getSession().getServletContext().setAttribute("userNumber", userNumber); ArrayList<User> userList = null; userList = (ArrayList<User>)httpSessionEvent.getSession().getServletContext().getAttribute("userList"); if(SessionUtil.getUserBySessionId(userList,httpSessionEvent.getSession().getId())!=null ){ userList.remove(SessionUtil.getUserBySessionId(userList, httpSessionEvent.getSession().getId())); } } }
RequestListener.java源代码:
package com.listener; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; import com.domain.User; import com.utils.SessionUtil; /** * Application Lifecycle Listener implementation class RequestListener * */ @WebListener public class RequestListener implements ServletRequestListener { private ArrayList<User> userList;//在线用户列表 /** * Default constructor. */ public RequestListener() { // TODO Auto-generated constructor stub } /** * @see ServletRequestListener#requestDestroyed(ServletRequestEvent) */ public void requestDestroyed(ServletRequestEvent servletRequestEvent) { } /** * @see ServletRequestListener#requestInitialized(ServletRequestEvent) */ public void requestInitialized(ServletRequestEvent servletRequestEvent) { userList = (ArrayList<User>) servletRequestEvent.getServletContext().getAttribute("userList"); if(userList==null){ userList = new ArrayList<User>(); } HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest(); String sessionIdString = request.getSession().getId(); if(SessionUtil.getUserBySessionId(userList,sessionIdString)==null){ User user = new User(); user.setSessionIdString(sessionIdString); user.setFirstTimeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); user.setIpString(request.getRemoteAddr()); userList.add(user); } servletRequestEvent.getServletContext().setAttribute("userList", userList); } }
SessionUtils.java源代码:
package com.utils; import java.util.ArrayList; import com.domain.User; public class SessionUtil { public static Object getUserBySessionId(ArrayList<User> userList,String sessionIdString) { for (int i = 0; i < userList.size(); i++) { User user = userList.get(i); if(user.getSessionIdString().equals(sessionIdString)){ return user; } } return null; } }
show.jsp页面:
<%@page import="com.domain.User"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>统计页面</title> </head> <body> <h1>当前在线用户人数统计</h1> <hr/> 当前在线用户人数:${userNumber}<br/> <% ArrayList<User> userList = (ArrayList<User>)request.getServletContext().getAttribute("userList"); if(userList!=null){ for(int i=0;i<userList.size();i++){ User user = userList.get(i); %> IP:<%=user.getIpString() %><br/> FirstTime:<%=user.getFirstTimeString() %><br/> SessionID:<%=user.getSessionIdString() %><br/> <% } } %> </body> </html>
运行结果:
对于过滤器和监听器的学习暂时告一段落,很推荐看的博文:
Java 过滤器:地址是:http://www.cnblogs.com/CBDoctor/p/4235082.html
Java 监听器:地址是:http://www.cnblogs.com/CBDoctor/p/4232859.html
javaweb学习总结(四十二)——Filter(过滤器)学习:地址是:http://www.cnblogs.com/xdp-
gacl/category/574705.html
javaweb学习总结(四十三)——Filter高级开发:地址是:http://www.cnblogs.com/xdp-
gacl/p/3948353.html
javaweb学习总结(四十四)——监听器(Listener)学习:地址是:http://www.cnblogs.com/xdp-
gacl/p/3961929.html
javaweb学习总结(四十五)——监听器(Listener)学习二:地址是:http://www.cnblogs.com/xdp-
gacl/p/3969249.html
javaweb学习总结(四十六)——Filter(过滤器)常见应用:地址是:http://www.cnblogs.com/xdp-
gacl/p/3948422.html
javaweb学习总结(四十七)——监听器(Listener)在开发中的应用:地址是:http://www.cnblogs.com/xdp-
gacl/p/3965508.html
大神的文章很受用,看的我直接爽翻了。
大牛的博客地址:http://www.cnblogs.com/xdp-gacl/