Share on Google+Share on Google+

IP Filter Example

The filter provides a basic security mechanism for a firewall to determining what traffic passes through the firewall based on IP address details.

IP Filter Example


The filter provides a basic security  mechanism for a firewall to determining what traffic passes through the firewall based on IP address details. This protects the secure network from outsiders. A filter is an object that perform filtering tasks on request and response. A FilterConfig object used by a servlet container used to pass information to a filter during initialization. Filters are registered in web.xml (deployment descriptor) of a web application. 
  The most easiest and effective way of minimize the risk from out side attacks is to filter incoming requests based on the IP address of the client. For example, if you have two web addresses that make requests using and and wish to restrict the servlet requests only from then following program will help you.

Here is the Source Code of

import java.util.*;
import javax.servlet.*;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class IPFilterExample implements Filter{
  public IPFilterExample() {}
  public final static String IP = "";
  private FilterConfig filterConfig;
  public void init(FilterConfig configthrows ServletException{
  this.filterConfig = config;
  public void doFilter(ServletRequest request, ServletResponse response,
  FilterChain filterchain
throws IOException, ServletException {
  PrintWriter out = response.getWriter();
  out.println("<html><head><title>IP Filter Example</title></head>");
  String userip = request.getRemoteAddr();
  HttpServletResponse httpResponse = null;
  if (response instanceof HttpServletResponse){
  httpResponse = (HttpServletResponseresponse;
  if (IP.equals(userip)) {
  httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN,"You are 
  not allowed to access the servlet!"
  else {
  filterchain.doFilter(request, response);
  out.println("<body><h3><font color='green'>Passed successfully
    from IP Filter<font></h3></body></html>"
  public void destroy() {}

Here is the source code of CallIpFilter Servlet.

import javax.servlet.*;
import javax.servlet.http.*;

public class CallIpFilter extends HttpServlet
  public void doGet(HttpServletRequest request, HttpServletResponse 
  PrintWriter pw = response.getWriter();
  pw.println("<head><title>IP Filter Example</title></title>");
  pw.println("<h1>Welcome, calling servlet successful</h1>");

Mapping of Filter (IPFilterExample) and Servlet (CallIpFilter) in web.xml


Running the servlet by this url: http://localhost:8080/ServletExample/CallIpFilter from IP The message will display as below:

But when, user access from IP address then he could not access the servlet (CallIpFilter) because IP Filter does not allow to access for this IP address and status report  will display as below:

Download Source Code


Posted on: July 5, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: IP Filter Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image