FileDocCategorySizeDatePackage
AuthenticationFilter.javaAPI DocExample1804Sun Sep 05 12:14:54 BST 2004com.oreilly.strutsckbk.ch11.ams

AuthenticationFilter

public class AuthenticationFilter extends Object implements Filter

Fields Summary
private String
onFailure
private FilterConfig
filterConfig
Constructors Summary
Methods Summary
public voiddestroy()

    
public voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        
        // if the requested page is the onFailure page continue
        // down the chain to avoid an infinite redirect loop        
        if (req.getServletPath().equals(onFailure)) {
            chain.doFilter(request, response);
            return;
        }
        
        HttpSession session = req.getSession(); // get the session or create it
        User user = (User) session.getAttribute("user");
        if (user == null) {
            // redirect to the login page 
            res.sendRedirect(req.getContextPath()+onFailure);
        }
        else {
            chain.doFilter(request, response);
        }
    
public voidinit(javax.servlet.FilterConfig filterConfig)

    
          
        this.filterConfig = filterConfig;
        onFailure = filterConfig.getInitParameter("onFailure");