FileDocCategorySizeDatePackage
IPFilter.javaAPI DocExample2001Wed Jul 30 16:08:30 BST 2003com.jspservletcookbook

IPFilter

public class IPFilter extends Object implements Filter

Fields Summary
private FilterConfig
config
public static final String
IP_RANGE
Constructors Summary
public IPFilter()

    
      
Methods Summary
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)

        
        String ip = request.getRemoteAddr();

        HttpServletResponse httpResp = null;
        
        if (response instanceof HttpServletResponse)
            httpResp = (HttpServletResponse) response;
            
        StringTokenizer toke = new StringTokenizer(ip,".");
        int dots = 0;
        String byte1 = "";
        String byte2 = "";
        String client = "";
        
        while (toke.hasMoreTokens()){
        
            ++dots;
            
            //if we've reached the second dot, break and check out the indx value    
            if (dots == 1){
            
                byte1 = toke.nextToken();
                
            } else {
            
                byte2 = toke.nextToken();
                break;
            }
        }//while
        
        //Piece together half of the client IP address so it can be compared with
		//the forbidden range represented by IPFilter.IP_RANGE
        client = byte1+"."+byte2; 
        
        if (IP_RANGE.equals(client)){
        
            httpResp.sendError(HttpServletResponse.SC_FORBIDDEN,
			    "That means goodbye forever!" );
            
        } else {
        
            chain.doFilter(request,response);
        }
        
    
public voidinit(javax.servlet.FilterConfig filterConfig)

    
      this.config = filterConfig;