如何把网站设为正确建设中,金融行业做网站,h5常用的编辑平台,seo优化费用展开全部读懂下面代码#xff0c;就知道如何实现 一个用户登陆 踢掉之前登陆的用户了//第一步// 此监听62616964757a686964616fe4b893e5b19e31333337626166器用来监听用户在对session做操作的时候执行相应的方法import javax.servlet.http.HttpSession;import javax.servlet.h…展开全部读懂下面代码就知道如何实现 一个用户登陆 踢掉之前登陆的用户了//第一步// 此监听62616964757a686964616fe4b893e5b19e31333337626166器用来监听用户在对session做操作的时候执行相应的方法import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import java.util.*;public class SessionListener implements HttpSessionListener ,HttpSessionAttributeListener{// 保存当前登录的所有用户public static Map loginUsernew HashMap();// 用这个作为session中的keypublic static String SESSION_LOGIN_NAME user_id_key;//session创建时调用这个方法public void sessionCreated(HttpSessionEvent arg0) {}//Session失效或者过期的时候调用的这个方法,public void sessionDestroyed(HttpSessionEvent se) {// 如果session超时, 则从map中移除这个用户try {loginUser.remove(se.getSession());}catch (Exception e) {e.printStackTrace();}}//执行setAttribute的时候, 当这个属性本来不存在于Session中时, 调用这个方法.public void attributeAdded(HttpSessionBindingEvent se) {// 如果添加的属性是用户名, 则加入map中if (se.getName().equals(SESSION_LOGIN_NAME)) {loginUser.put(se.getSession(), Long.valueOf(se.getValue().toString()));}}//当执行removeAttribute时调用的方法public void attributeRemoved(HttpSessionBindingEvent se) {// 如果移除的属性是用户名, 则从map中移除if (se.getName().equals(SESSION_LOGIN_NAME)) {try {loginUser.remove(se.getSession());} catch (Exception e) {}}}//当执行setAttribute时 ,如果这个属性已经存在, 覆盖属性的时候, 调用这个方法public void attributeReplaced(HttpSessionBindingEvent se) {// 如果改变的属性是用户名, 则跟着改变mapif (se.getName().equals(SESSION_LOGIN_NAME)) {loginUser.put(se.getSession(), Long.valueOf(se.getValue().toString()));}}//别忘了到你的web.xml中去配置一下listener//第二步//写一个判断用户是否已经登陆的方法public boolean isLogonUser(Long userId) {Set keys SessionListener.loginUser.keySet();for (HttpSession key : keys) {if (SessionListener.loginUser.get(key).equals(userId)) {return true;}}return false;}//第三步//在用户登陆的action.method或者是loginServlet.doGet/doPost中//判断用户名、密码都OK后再调用第二步的方法参数为用户IDtrue则表示该用户已经登陆//第四步//用户窗口关闭/或者用户退出的时候*一定要 request.getSession().invalidate()//用户窗口关闭js//关闭窗口时调用此方法function window.onunload(){if((window.screenLeft10000 window.screenTop10000)||event.altKey){//清除当前session,使用jquery 提供的方法$.post(${base}/ClearSession.wp);// [ ${base}/ClearSession.wp ]这是一个请求//请求到自己写的ClearSessionServlet// 在此ClearSessionServlet中重写doPost方法// 内容为 request.getSession().invalidate()}