过滤器、监听器常见应用
- 用户登录之后,向Session中放入用户的数据
- 进入主页的时候要判断用户是否已经登陆,要求:在过滤器中实现.
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (request.getSession().getAttribute("USER_SESSION") == null ){
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
filterChain.doFilter(request, response);
}