IP filter ver. 1.9


This is a Java servlet filter (as per Servlet API 2.3). This filter lets you redirect incoming requests depending on user's IP address. So, you may use it for example as an access restriction tool, for load balancing etc.

Package contains the filter itself (RouteFilter) and servlet (RouteServlet) you may use for interactive data management.

How to use it:

a) download ipflt.jar and save it in WEB-INF/lib

b) create data file with routing information. It is a text file, each line describes an action for one group of IP's (or for one IP). Empty lines and any line starts with // or # are ignored. Path to this file should be provided as a parameter for both RouteFilter and RouteServlet. Action looks like:
 


IP address \t some_url

or

IP address1 - IP address2 \t some_url

where \t is a tabulator. For example:
 


192.168.0.5   http://www.acme.com
192.168.1.0 - 192.168.255.0   /index1.htm

Instead of URL you can use the reserverd word none. In this case request will be processed "as is".

Actually you can use RouteServlet for creating/updating such file (files).

c) describe this filter in web.xml.
 


<filter>
  <filter-name>RouteFilter</filter-name>
  <filter-class>com.cj.ip.RouteFilter</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>your_data_file</param-value>
  </init-param>
</filter>

You can add an optional initial parameter: default. It describes an URL for redirect request from IP addresses not included into config file. By default such requests will be processed "as is". E.g.:
 


<filter>
  <filter-name>RouteFilter</filter-name>
  <filter-class>com.cj.ip.RouteFilter</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>your_data_file</param-value>
  </init-param>
  <init-param>
    <param-name>default</param-name>
    <param-value>http://some_url_here</param-value>
  </init-param>
</filter>

d) describe a mapping for this filter in web.xml
 


<filter-mapping>
  <filter-name>RouteFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>

in this case filter will be on for the each .jsp file

And now when some .jsp page is requested RouteFilter may redirect request to the specified URL. If target URL starts with http than filter redirects request, otherwise filter assumes a local resource and forwards request.

e) describe RouteServlet you will use for access to your data_file:
 


<servlet>
  <servlet-name>RouteServlet</servlet-name>
  <servlet-class>com.cj.ip.RouteServele</servlet-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>your_data_file</param-value>
  </init-param>
</servlet>

Now you may run this servlet http://your_host/servlet/RouteServlet and work with your data file.

   For downloading:

    IP package:  ipflt.jar
 

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

     

Also in JSOS: