• listener单点登录和在线人数


    1,jsp提交一个form

    2,把收集到的数据放到一个personInfo类中,personInfo类放进session中

    3,添加的时候触发listener,把一个personInfo的account为key ,session为value,放进一个map中,如果在map找到了这个session,

        证明他已经登陆了,把他弄下线。map重新添加


    计算在线人数用一个装满常量的类

    1,map的size()就是在线的人数

    登陆页面jsp

    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@ page import="listen.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)) {
            System.out.println("outout");
            session.removeAttribute("personInfo");
            response.sendRedirect(response.encodeRedirectURL(request
                    .getRequestURI()));
            return;
        }
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
        <%
            if (session.getAttribute("personInfo") != null) {
        %>
        欢迎你, ${personInfo.account }.
        <br /> 你的登陆ip, ${personInfo.ip }.
        <br /> 登陆时间 , ${personInfo.loginDate }.
        <a href="${contextPage.request.requestURI }?action=logout">退出</a>
        <script>
            setTimeout("location=location;", 5000);
        </script>
        <%
            } else {
        %>
        ${msg }
        <form action="${contextPage.request.requestURI }?action=login"
            method="post">
            账号: <input name="account" /> <input type="submit" value="登陆">
            <from />
    
            <%
                }
            %>
        
    </body>
    </html>
    View Code

    单点登录

    package listen;
    
    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>();
    
        @Override
        public void attributeAdded(HttpSessionBindingEvent event) {
            // TODO Auto-generated method stub
            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() + "被迫下线");
                    session.removeAttribute("personInfo");
                    session.setAttribute("msg", "你的账号被其他机器登陆了");
                }
                map.put(personInfo.getAccount(), event.getSession());
                log.info("账号:" + personInfo.getAccount() + "在" + personInfo.getIp()
                        + "登陆");
            }
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent event) {
            // TODO Auto-generated method stub
            String name = event.getName();
            if ("personInfo".equals(name)) {
                PersonInfo personInfo = (PersonInfo) event.getValue();
                log.info("账号" + personInfo.getAccount() + "注销");
            }
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent event) {
            // TODO Auto-generated method stub
    
        }
    
    }
    View Code

    在线人数

    package listen;
    
    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;
    
    public class MySessionListener implements HttpSessionListener,
            HttpSessionAttributeListener {
    
        @Override
        public void sessionCreated(HttpSessionEvent se) {
            // TODO Auto-generated method stub
            Count.TOTAL_HISTORY_COUNT++;// 总访问人数加一
            Count.SESSION_MAP.put(se.getSession().getId(), se.getSession());// 放进去
            if (Count.SESSION_MAP.size() > Count.MAX_ONLINE_COUNT) {
                Count.MAX_ONLINE_COUNT = Count.SESSION_MAP.size();
            }
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            // TODO Auto-generated method stub
            HttpSession session = se.getSession();
            Count.SESSION_MAP.remove(session.getId());
        }
    
        @Override
        public void attributeAdded(HttpSessionBindingEvent event) {
            // TODO Auto-generated method stub
            if ("personInfo".equals(event.getName())) {
                Count.CURRENT_LOGIN_COUNT++;// 当前在线人数加一
                HttpSession session = event.getSession();
            }
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent event) {
            // TODO Auto-generated method stub
            if (event.getName().equals("personInfo")) {
                Count.CURRENT_LOGIN_COUNT--;
            }
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent event) {
            // TODO Auto-generated method stub
    
        }
    
    }
    View Code

  • 相关阅读:
    OOP & Pointer: Segment Tree
    ICPC_2020 上海站
    Notes: Kirchhoff's Matrix 基尔霍夫矩阵
    CS61A Homework: Church Numerals
    题解:[COCI2011-2012#5] BLOKOVI
    题解:SDOI2017 新生舞会
    题解:POI2012 Salaries
    题解:洛谷P1357 花园
    题解:CF593D Happy Tree Party
    题解 P2320 【[HNOI2006]鬼谷子的钱袋】
  • 原文地址:https://www.cnblogs.com/vhyc/p/6548495.html
Copyright © 2020-2023  润新知