session监听器怎样得到request和response

12-03-31 363160696
当session失效时就跳到登录页面,于是我想到,当sesson销毁时跳到登录页面,于是我建了一个session监听器,想在requestDestroyed方法时跳转到登录页面,但是我在这个类里面根本得不到request和response对象,怎样得到这两个对象呢。这是web.xml中的配置

<!-- session超时定义,单位为分钟 -->

<session-config>

<session-timeout>20</session-timeout>

</session-config>

如果这样不行,可不可以通过配置跳转到登录页面

4
363160696
2012-03-31 15:44
event.getSession().getServletContext().getRequestDispatcher("/403.jsp").forward(request, response);补充一下,就是这两个参数不知怎样得到,

banq
2012-03-31 16:06
通过其他方式实现,建立一个SessionOutFilter:

public class SessionTimeout implements Filter{
   RequestDispatcher rd = null;

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
   throws IOException, ServletException{
      HttpServletRequest req = (HttpServletRequest)request;
      HttpSession session = req.getSession();

   // New Session so forward to login.jsp
   //因为前面一个Session已经失效,如果还在这个站点,必然是新创立的Session
   if (session.isNew()){
     requestDispatcher = request.getRequestDispatcher("/login/login.jsp");
     requestDispatcher.forward(request, response);
  }

  // Not a new session so continue to the requested resource
   else{
      filterChain.doFilter(request, response);
  }
}

public void init(filterConfig arg) throws ServletException{}
public void destroy(){}

}
<p>

http://stackoverflow.com/questions/2198486/display-message-after-session-timeout

猜你喜欢