0

我正在尝试使用 HttpSessionListener 检查 cookie 并获取请求的 IP 地址。

但是,我无权访问侦听器中的 HttpServletRequest 以传递给 STKUserCookie 或获取 IP。

public STKUserCookie(HttpServletRequest request)

public void sessionCreated(HttpSessionEvent se) {
    HttpSession httpSes = se.getSession();

    if ( se.getSession().getAttribute("STKUserSession") == null) {
        STKUserCookie userCookie = new STKUserCookie(request);  <------- ERROR on this line "request" not available
        String userBadge = userCookie.getUserID();
        STKUserDAO userDAO = new STKUserDAO();
        STKUser user = userDAO.getUser(userBadge);
        if (user != null) {
            user.setIpAddress(se.getRemoteAddr());    <------- ERROR on this line "getRemoteAddr" not a method of se
            userDAO.updateLogin(user);
            httpSes.setMaxInactiveInterval(36000); //set to 10 hours
            httpSes.setAttribute("STKUserSession", user);
        }

    }
}

上面曾经是一个脚本,我包含在我的所有 jsp 页面中,我试图将它重构为一个侦听器,而不是一个过滤器,因为我只希望每个会话调用一次以减少开销。还是我不应该担心开销并将方法粘贴到过滤器中?

4

1 回答 1

1

不要担心无操作过滤器的开销。这是微不足道的。

于 2010-12-10T19:04:41.730 回答