MyEclipse中新建Web Project項目,完整目錄以下:html
須要的jar包爲commons-logging-xxx.jarjava
一、singleton.jspweb
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <jsp:directive.page import="com.wang.singleton.PersonInfo"/> <% String action = request.getParameter("action"); String account = request.getParameter("account"); if("login".equals(action) && account.trim().length()>0){ PersonInfo personInfo = new PersonInfo(); personInfo.setAccount(account); personInfo.setIp(request.getRemoteAddr()); personInfo.setLoginDate(new java.util.Date()); session.setAttribute("personInfo",personInfo); response.sendRedirect(response.encodeRedirectURL(request.getRequestURI())); return; } else if("logout".equals(action)){ session.removeAttribute("personInfo"); response.sendRedirect(response.encodeRedirectURL(request.getRequestURI())); return; } %> <!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>Insert title here</title> </head> <body> <c:choose> <c:when test="${ personInfo != null}"> 歡迎您,${personInfo.account}<br/> 您的登陸ip爲,${personInfo.ip}<br> 登陸時將爲,<fmt:formatDate value="${personInfo.loginDate}" pattern="yyyy-MM-dd HH:mm"/><br/> <a href="${pageContext.request.requestURI}?action=logout">退出</a> <!-- 每五秒鐘刷新一次頁面 --> <script>setTimeout("location=location;", 5000);</script> </c:when> <c:otherwise> ${msg} <c:remove var="msg" scope="session"/> <form action="${pageContext.request.requestURI}?action=login" method="post"> 帳號:<input name="account"> <input type="submit" value="登陸"> </form> </c:otherwise> </c:choose> </body> </html>
二、PersonInfo.javaapache
package com.wang.singleton; import java.io.Serializable; import java.util.Date; public class PersonInfo implements Serializable{ private static final long serialVersionUID = 1L; private String account; private String ip; private Date loginDate; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public Date getLoginDate() { return loginDate; } public void setLoginDate(Date loginDate) { this.loginDate = loginDate; } public boolean equals(Object obj){ if(obj == null || !(obj instanceof PersonInfo)){ return false; } return account.equalsIgnoreCase(((PersonInfo) obj).getAccount()); } }
三、LoginSessionListener.java瀏覽器
package com.wang.singleton; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class LoginSessionListener implements HttpSessionAttributeListener { Log log= LogFactory.getLog(this.getClass()); Map<String,HttpSession> map = new HashMap<String,HttpSession>(); public LoginSessionListener() { // TODO Auto-generated constructor stub } public void attributeRemoved(HttpSessionBindingEvent event) { // 刪除屬性前被調用 String name = event.getName(); if(name.equals("personInfo")){ PersonInfo personInfo = (PersonInfo) event.getValue(); map.remove(personInfo.getAccount()); log.info("帳號"+personInfo.getAccount()+"註銷"); } } public void attributeAdded(HttpSessionBindingEvent event) { // 添加session時被調用 String name = event.getName(); if(name.equals("personInfo")){ PersonInfo personInfo = (PersonInfo) event.getValue(); if(map.get(personInfo.getAccount()) != null){ HttpSession session = map.get(personInfo.getAccount()); PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo"); log.info("帳號"+oldPersonInfo.getAccount()+"在"+oldPersonInfo.getIp()+"已經登陸,該登陸將被迫下線!"); session.removeAttribute("personInfo"); session.setAttribute("msg", "您的帳號已經在其餘機器上登陸,您被迫下線!"); } map.put(personInfo.getAccount(), event.getSession()); log.info("帳號"+personInfo.getAccount()+"在"+personInfo.getIp()+"登陸"); } } public void attributeReplaced(HttpSessionBindingEvent event) { // 修改屬性時被調用 String name = event.getName(); if(name.equals("personInfo")){ PersonInfo oldPersonInfo = (PersonInfo) event.getValue(); //移除舊的登陸信息 map.remove(oldPersonInfo.getAccount()); //新的登陸信息 PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo"); //也要檢查新的帳號是否在別的機器上登陸 if(map.get(personInfo.getAccount()) != null){ HttpSession session = map.get(personInfo.getAccount()); session.removeAttribute("personInfo"); session.setAttribute("msg", "您的帳號已經在其餘機器上登陸,您被迫下線!"); } map.put(personInfo.getAccount(), event.getSession()); log.info("帳號"+personInfo.getAccount()+"在"+personInfo.getIp()+"登陸"); } } }
實現效果session
一、在谷歌瀏覽器隨便輸入一串英文jsp
二、顯示以下post
三、換個IE瀏覽器,輸入相同一串英文,原來登陸的就被擠掉了ui
參考:《javaweb王者歸來》this