简体   繁体   中英

Java: Using filter (user logged/user not logged)

I need to write a code, that will be redirecting to different *.jsp sites depending on whether user is logged on or not logged on. I found a hint, that I can use filter to do it and I need to use doFilter or/along with init methods. Any ideas?

public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
    }
public void init(FilterConfig config) throws ServletException {

    }

This is a very basic sample...but let's suppose that the login proces set in session an attribute called "user" in the doFilter method you can do something like this

if( request.getSession().getAttribute("user") == null )
{
//User not logged...redirect
}
else
{
//Normal filter execution
}

init() method will be called on Filter's initialization and doFilter() will be called when a request is made and Filter is mapped to filter those request


Related:

For an example, see Filters Tutorial , particularly the section titled Authentication with Filters. (There's a typo that actually makes this say "Authentication with Filers" but that is the section I am referring to...obviously it is supposed to say filters :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM