java-監聽器(Listener)

監聽器:用於監聽web應用中某些對象、信息的建立、銷燬等動做,服務器會自動調用相應的方法進行處理。經常使用於統計在線人數,初始化系統參數等。java

Javaweb監聽器主要監聽對象有ServletContextHttpSessionServletRequestweb

 

下面是貼上一個統計登陸人信息的栗子:服務器

  在web.xml 中配置監聽器:session

<!--用戶登陸監聽器-->
    <listener>
        <listener-class>com.xxx.listener.SessionListener</listener-class>
    </listener>

  建立監聽器:ide

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.*;

public class SessionListener implements HttpSessionListener,ServletRequestListener {
    // 定義監聽類對象,用於存儲全部用戶的登陸信息
    private static Map<String,String> userList = new HashMap<String, String>();
    // 定義session  map對象
    private static Map<String,HttpSession> sessionMap = new HashMap<String, HttpSession>();

    private HttpServletRequest request = null;

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {// 監聽 session 建立

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {// 監聽 session 銷燬
        HttpSession httpSession = httpSessionEvent.getSession();
        // 獲取登陸用戶名
        String username = (String) httpSession.getAttribute("username");
        if(username != null){
            Iterator iterator = userList.entrySet().iterator();
            Map map = null;
            String logmsg = null;
            // 遍歷 userList:將userlist中的用戶信息刪除
            while (iterator.hasNext()){
                map = (Map) iterator.next();
                if(map.get(username) != null){
                    logmsg = (String) map.get(username);
                    if(logmsg.indexOf(request.getLocalAddr()) > -1){
                        userList.remove(username);
                        sessionMap.remove(username);
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {// 監聽 request 銷燬

    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {// 監聽 request 建立
        this.request = (HttpServletRequest) servletRequestEvent.getServletRequest();
        HttpSession httpSession = this.request.getSession();
        // 獲取登陸用戶名
        String username = (String) httpSession.getAttribute("username");
        if(username != null){
            // 若是 userList爲空直接 put
            if(userList.isEmpty()){
                userList.put(username,"IP: " + request.getLocalAddr() + " ,logtime:" + ProUtil.getStrDatetime(new Date()));
                sessionMap.put(username,httpSession);
                return;
            }
            String logmsg = null;
            if(userList.get(username) == null){
                userList.put(username,"IP: " + request.getLocalAddr() + " ,logtime:" + ProUtil.getStrDatetime(new Date()));
                sessionMap.put(username,httpSession);
            }else {
                logmsg = userList.get(username);
                if(logmsg.indexOf(request.getLocalAddr()) > -1){
                    userList.put(username,"IP: " + request.getLocalAddr() + " ,logtime:" + ProUtil.getStrDatetime(new Date()));
                    sessionMap.put(username,httpSession);
                }else {
                    userList.put(username,"IP: " + request.getLocalAddr() + " ,logtime:" + ProUtil.getStrDatetime(new Date()));
                    sessionMap.get(username).setAttribute("logError","該帳號已經在其餘地方登陸!!!");
                    sessionMap.put(username,httpSession);
                }
            }
        }
    }
}
相關文章
相關標籤/搜索