【发布时间】:2011-05-23 16:00:49
【问题描述】:
我正在尝试使用 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 页面中,我试图将它重构为一个侦听器,而不是一个过滤器,因为我只希望每个会话调用一次以减少开销。还是我不应该担心开销并将方法粘贴到过滤器中??
【问题讨论】:
-
SessionListener 是错误的地方。将其放入过滤器中。过滤器可以直接影响 HttpServeletRequest 的处理。
标签: java servlets servlet-filters servlet-listeners