applicazioni Web Java HttpServletRequest in un HttpSessionListener
-
08-10-2019 - |
Domanda
Sto cercando di utilizzare un HttpSessionListener per verificare la presenza di un cookie e ottenere l'indirizzo IP della richiesta.
Comunque non hanno accesso alla HttpServletRequest nell'ascoltatore da passare al STKUserCookie o per ottenere l'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);
}
}
}
È possibile che questo era uno scriptlet che mi è stato anche in tutte le mie pagine JSP e sto cercando di refactoring in un ascoltatore, piuttosto che un filtro da quando io lo voglio solo per essere chiamato una volta per sessione per ridurre il sovraccarico. O dovrei Non preoccuparti per la testa e il bastone il metodo in un filtro ??
Soluzione
Non si preoccupi per il sovraccarico di un filtro no-op. Si è trascurabile.