开发者

Java web application HttpServletRequest in an HttpSessionListener

开发者 https://www.devze.com 2023-01-30 04:10 出处:网络
I am trying to use a HttpSessionListener to check for a cookie and get the IP address of the request.

I am trying to use a HttpSessionListener to check for a cookie and get the IP address of the request.

However I don't have access to the HttpServletRequest in the listener to pass to STKUserCookie or to get the IP.

public STKUserCookie(HttpServletRequest request)

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

    if ( se.getSession().getAttribute("STKUserSession") == null) {
        STKUserCookie userCookie = new STKUserCookie(开发者_JAVA百科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);
        }

    }
}

The above used to be a scriptlet that I was including in all my jsp pages and I am trying to refactor it into a listener, rather than a filter since I only want it to be called once per session to reduce overhead. Or should I not worry about the overhead and stick the method into a filter??


Don't worry about the overhead of a no-op filter. It is negligible.

0

精彩评论

暂无评论...
验证码 换一张
取 消