FileDocCategorySizeDatePackage
BlockFilter.javaAPI DocExample1659Tue Jun 10 12:21:46 BST 2003com.jspservletcookbook

BlockFilter

public class BlockFilter extends Object implements Filter

Fields Summary
private FilterConfig
config
Constructors Summary
public BlockFilter()
Creates new BlockFilter

Methods Summary
private booleanauthenticateUser(java.lang.String userName)

    
        //authenticate the user using JNDI and a database, for instance
        return false;
    
    
public voiddestroy()

        /*called before the Filter instance is removed 
        from service by the web container*/
    
public voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)

 
    HttpServletRequest req = null;
    boolean authenticated = false;
    PrintWriter out = null;
     
    if (request instanceof HttpServletRequest){
        
         req = (HttpServletRequest) request;
         String user = req.getParameter("user");
         authenticated = authenticateUser(user);
        }

        if (authenticated){
            chain.doFilter(request,response);
        } else {
            
          response.setContentType("text/html");
          out = response.getWriter();
          out.println("<html><head><title>Authentication Response</title></head><body>");
          out.println("<h2>Sorry your authentication attempt failed</h2>");
          
          out.println("</body></html>");
          out.close();
            
        }
    
public voidinit(javax.servlet.FilterConfig filterConfig)

    
        this.config = filterConfig;