Principle: In addition to the landing page, the other pages are virtual path with a unified identity symbol. Such as the use / Login
. The filter will intercept all virtual path begins with / user pages. Verify that the filter page request corresponding session where there should not have landed after a successful content. If not, redirect to the landing page. If so, prove already landed, the implementation of doFilter method filterChain.
filter代码:
public class Myfilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//先转为HttpServletRequest,HttpServletResponse
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response=(HttpServletResponse)servletResponse;
//如果前面已经登陆过,在session里赋值了,该页面可以执行下一个servlet,如果没有先重定向到登陆页面!
if (request.getSession().getAttribute("loginUser")==null){
response.sendRedirect(request.getContextPath()+"/login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
Put the code into the session content of the success of landing:
Finally, do not forget to web.xml configuration file for the filter. Or directly with annotations marked @WebFilter ( "/ user")
Well, simply use filter verification is complete.