filter to intercept implement user is not logged in

@WebFilter ( "/ *")   // annotation path filter configured 
public  class LoginFilter the implements the Filter {

    @Override
    public  void the doFilter (the ServletRequest servletRequest, the ServletResponse ServletResponse, the FilterChain catena alberghiera) throws IOException, ServletException {
         // 1. Get HttpServletRequest object 
        HttpServletRequest Request = (HttpServletRequest) servletRequest;
         // Get access path 
        String URI = Request.getRequestURI ();
         // determine if the access path is to go to the login screen release 
        IF (uri.contains ( "/ the login.jsp") || uri.contains ( "/ loingServlet.action" )) {
            chain.doFilter(servletRequest, servletResponse);
        }else{
            User the User = (the User) Request.getSession () the getAttribute ( "User." );
             // if the user is present, the release 
            IF (= User! Null ) {
                chain.doFilter(servletRequest, servletResponse);
            } The else {
                 // otherwise forwarded the login page, the prompt message 
                request.setAttribute ( "login-msg", " the page, the need to login to access" );
                request.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);
            }
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/jasonjson/p/12410432.html