java web項目中管理session的時候,除了在web.xml文件中設置過期時間html
<session-config>
<session-timeout>2</session-timeout>
</session-config>
xxx分鐘,還能夠經過
HttpSessionListener
接口或HttpSessionBindingListener接口來實現對session的監控,經過監聽session的新建和銷燬來控制
實現HttpSessionListener接口:java
package listener; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class OnlineListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { System.out.println("新建session:" + event.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 取得登陸的用戶名 String username = (String) session.getAttribute("username"); // 從在線列表中刪除用戶名 List onlineUserList = (List) application.getAttribute("onlineUserList"); onlineUserList.remove(username); System.out.println(username+"已經退出!"); } }
後要在web.xml中配置監聽接口配置:web
<listener> <listener-class>listener.OnlineListener</listener-class> </listener>
當用戶首次訪問網站頁面的時候,框架就會生成一個sessionID這時執行listener.OnlineListener.sessionCreated()方法,執行操做,不是對單個session與用戶的是對整個全局的session的操做session
項目其餘文件代碼:
index.jspapp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <form action="httpSessionLogin.jsp" method="get"> HttpSessionListener方式監聽Session:<br/> 登錄:<input id="username" name="username"/> <input type="submit"/> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="listener.OnlineListener"%> <% request.setCharacterEncoding("UTF-8"); // 取得登陸的用戶名 String username = request.getParameter("username"); // 把用戶名保存進session System.out.println("jspsession:" + session.getId()); session.setAttribute("username", username); session.setAttribute("onlineUserList", new OnlineListener(username)); System.out.println("用戶:"+username+" 登錄成功"); // 把用戶名放入在線列表 List onlineUserList = (List) application.getAttribute("onlineUserList"); // 第一次使用前,須要初始化 if (onlineUserList == null) { onlineUserList = new ArrayList(); application.setAttribute("onlineUserList", onlineUserList); } onlineUserList.add(username); // 成功 response.sendRedirect("result.jsp"); %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> 登錄成功 <h3>您好:${username} [<a href="logout.jsp">註銷</a>]</h3> 當前在線用戶: <table> <% List onlineUserList = (List) application.getAttribute("onlineUserList"); for (int i = 0; i < onlineUserList.size(); i++) { String onlineUsername = (String) onlineUserList.get(i); %> <tr> <td><%=onlineUsername%></td> <td><a href="deleteUser.jsp?username=<%=onlineUsername%>">刪除</a></td> </tr> <% } %> </table> </body> </html>
而HttpSessionBindingListener運用時不用在web.xml中配置,HttpSessionBindingListener雖然叫作監聽器,但使用方法與HttpSessionListener徹底不一樣。咱們實際看一下它是如何使用的。新建類OnlineUserBindingListener,實現HttpSessionBindingListener接口,構造方法傳入username參數,HttpSessionBindingListener內有兩個方法valueBound(HttpSessionBindingEvent event)和valueUnbound(HttpSessionBindingEvent event),前者爲數據綁定,後者爲取消綁定所謂對session進行數據綁定,就是調用session.setAttribute()把HttpSessionBindingListener保存進session中框架
index.jspjsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <form action="sessionBindingLogin.jsp" method="get"> HttpSessionBindingListener方式監聽Session:<br/> 登錄:<input id="username" name="username"/> <input type="submit"/> </form> </body> </html>
sessionBindingLogin.jsp:網站
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="listener.OnlineBindingListener"%> <% request.setCharacterEncoding("UTF-8"); // 取得登陸的用戶名 String username = request.getParameter("username"); // 把用戶名放入在線列表 session.setAttribute("onlineUserBindingListener", new OnlineBindingListener(username)); // 成功 response.sendRedirect("result.jsp"); %>
package listener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class OnlineBindingListener implements HttpSessionBindingListener { String username; public OnlineBindingListener(String username){ this.username=username; } public void valueBound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); System.out.println("建立Session:"+session.getId()); System.out.println("username:"+username); session.setAttribute("username", username); ServletContext application = session.getServletContext(); // 把用戶名放入在線列表 List onlineUserList = (List) application.getAttribute("onlineUserList"); Map<String, HttpSession> sessionMap = (Map<String, HttpSession>)application.getAttribute("sessionMap"); // 第一次使用前,須要初始化 if (onlineUserList == null) { onlineUserList = new ArrayList(); application.setAttribute("onlineUserList", onlineUserList); } if (sessionMap == null) { sessionMap = new HashMap<String, HttpSession>(); application.setAttribute("sessionMap", sessionMap); } onlineUserList.add(this.username); sessionMap.put(username, session); } public void valueUnbound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 從在線列表中刪除用戶名 List onlineUserList = (List) application.getAttribute("onlineUserList"); onlineUserList.remove(this.username); System.out.println("Session實效:"+session.getId()); System.out.println(this.username + "退出。"); } }
deleteUser.jspui
<%@ page language="java" import="java.util.*,javax.servlet.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); // 取得登陸的用戶名 String username = request.getParameter("username"); System.out.println(username); Map<String, HttpSession> sessionMap = (Map<String, HttpSession>)application.getAttribute("sessionMap"); HttpSession otherSession = (HttpSession)sessionMap.get(username); //otherSession.removeAttribute("username"); otherSession.invalidate(); sessionMap.remove(username); // 成功 response.sendRedirect("result.jsp"); %>