Question

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(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??

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top