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
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 192.168.10.146 and 127.0.0.1 and wish to restrict the servlet requests only from 127.0.0.1 then following program will help you.
Here is the Source Code of IPFilterExample.java
Here is the source code of CallIpFilter Servlet.
Mapping of Filter (IPFilterExample) and Servlet (CallIpFilter) in web.xml
Running the servlet by this url: http://localhost:8080/ServletExample/CallIpFilter from IP 188.8.131.52. The message will display as below:
But when, user access from IP address 127.0.0.1 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:
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial