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 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
import java.io.*;
|
Here is the source code of CallIpFilter Servlet.
import java.io.*;
|
Mapping of Filter (IPFilterExample) and Servlet (CallIpFilter) in web.xml
| <filter> <filter-name>IPFilterExample</filter-name> <filter-class>IPFilterExample</filter-class> </filter> <filter-mapping> <filter-name>IPFilterExample</filter-name> <url-pattern>/CallIpFilter</url-pattern> </filter-mapping> <servlet> <servlet-name>CallIpFilter</servlet-name> <servlet-class>CallIpFilter</servlet-class> </servlet> <servlet-mapping> <servlet-name>CallIpFilter</servlet-name> <url-pattern>/CallIpFilter</url-pattern> </servlet-mapping> |
Running the servlet by this url: http://localhost:8080/ServletExample/CallIpFilter from IP 193.168.10.146. 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 are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: IP Filter Example View All Comments
Post your Comment