JavaWeb_初识监听器Listener
监听器(listener):对项目起到监听的作用,它能感知到包括request(请求域),session(会话域)和applicaiton(应用程序)的初始化和属性的变化
监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext、HttpSession和ServletRequest等域对象的创建和销毁事件,它还可以监听域对象的属性发生修改的事件,可以在事件发生前或者发生后做一些必要的处理
这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同
session和application监听器
application监听器
package com.Gary.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class ApplicationListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("application被销毁出来"); } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("application被创建出来,这里可以做一些初始化的事情"); } }
package com.Gary.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class SessionListener implements HttpSessionListener { public SessionListener() { // TODO Auto-generated constructor stub } public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub System.out.println("sessionCreated被创建"); } public void sessionDestroyed(HttpSessionEvent arg0) { // TODO Auto-generated method stub System.out.println("sessionCreated被销毁"); } }
package com.Gary.listener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; @WebListener public class RequestListener implements ServletRequestListener { public RequestListener() { // TODO Auto-generated constructor stub } public void requestDestroyed(ServletRequestEvent arg0) { System.out.println("requestDestroyed"); } public void requestInitialized(ServletRequestEvent arg0) { // TODO Auto-generated method stub System.out.println("requestInitialized"); } }
配置监听器方式
监听器申明
@WebListener
web.xml中配置监听器
<listener> <listener-class></listener-class> </listener>
<!-- <listener> --> <!-- <listener-class>com.Gary.listener.SessionListener</listener-class> --> <!-- </listener> --> <!-- Session超过1分钟后,服务器会自动放弃Session信息 默认三十分钟--> <session-config> <session-timeout>1</session-timeout> </session-config>
application的属性监听器
当整个项目启动时,系统会往application中塞一些东西调用一次attributeAdded(),进入attributelistener.jsp后若不存在application.setAttribute("number",100)时候会再一次调用attributeAdded(),当存在application.setAttribute("number",100)时系统会调用attributeReplaced()
ApplicationAttributeListener用来监听application.setAttribute()和application.removeAttribute()
同理session和requset
<% application.setAttribute("number",100); // application.removeAttribute("number"); %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% application.setAttribute("number",100); // application.removeAttribute("number"); %> </body> </html>
package com.Gary.listener; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.annotation.WebListener; @WebListener public class ApplicationAttributeListener implements ServletContextAttributeListener{ @Override public void attributeAdded(ServletContextAttributeEvent arg0) { // TODO Auto-generated method stub System.out.println("attributeAdded"); } @Override public void attributeRemoved(ServletContextAttributeEvent arg0) { // TODO Auto-generated method stub System.out.println("attributeRemoved"); } @Override public void attributeReplaced(ServletContextAttributeEvent arg0) { // TODO Auto-generated method stub // arg0.getName(); System.out.println("attributeReplaced"); } }
Session实现单点登录(备份)
同一用户登录同一网址时会把前一个用户挤下来
public class SessionManager { public static Map<String,HttpSession> sessionMap = new HashMap<String,HttpSession >(); }
public void attributeAdded(HttpSessionBindingEvent arg0) { if(arg0.getName().equals("user")) { User u = (User) arg0.getValue(); //判断是否有其他客户端已经登录 if(SessionManager.sessionMap.containsKey(u.getUsername())) { //获得另外一个客户端的session HttpSession session = SessionManager.sessionMap.get(u.getUsername()); //把登录信息从这个客户端抹除 session.removeAttribute("user"); //从session集合里面移除对应的用户名 SessionManager.sessionMap.remove(u.getUsername()); } SessionManager.sessionMap.put(u.getUsername(), arg0.getSession()); } }
package com.sikiedu.util; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSession; public class SessionManager { public static Map<String,HttpSession> sessionMap = new HashMap<String,HttpSession >(); }
package com.sikiedu.listener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import com.sikiedu.model.User; import com.sikiedu.util.SessionManager; /** * Application Lifecycle Listener implementation class SessionAttributeListener * */ @WebListener public class SessionAttributeListener implements HttpSessionAttributeListener { /** * Default constructor. */ public SessionAttributeListener() { // TODO Auto-generated constructor stub } /** * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent) */ public void attributeAdded(HttpSessionBindingEvent arg0) { if(arg0.getName().equals("user")) { User u = (User) arg0.getValue(); //判断是否有其他客户端已经登录 if(SessionManager.sessionMap.containsKey(u.getUsername())) { //获得另外一个客户端的session HttpSession session = SessionManager.sessionMap.get(u.getUsername()); //把登录信息从这个客户端抹除 session.removeAttribute("user"); //从session集合里面移除对应的用户名 SessionManager.sessionMap.remove(u.getUsername()); } SessionManager.sessionMap.put(u.getUsername(), arg0.getSession()); } } /** * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent) */ public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub System.out.println("attributeRemoved"); } /** * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent) */ public void attributeReplaced(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub System.out.println("sessionattributeReplaced"); } }