Creating a simple Filter for a J2EE Java Web App.


IPFilter.java source; (Filter)

package com.web.filterexample;

import java.io.IOException;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IPFilter implements Filter {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest)servletRequest;

HttpServletResponse response = (HttpServletResponse)servletResponse;

String IP = request.getRemoteAddr(); //getting user’s IP address

System.out.println(“Your IP address is>>> ” + IP);

if(IP.startsWith(“10.36.”))
{
//Nothing to do here
//allowing user to enter the system
//continue to filter by calling doFilter method

filterChain.doFilter(servletRequest, servletResponse);
}
else //other IP addresses are not allowed to use the WEB application
{
response.sendError(403, “You are not authorized to enter the web site (SERVER talks)”);

filterChain.doFilter(servletRequest, servletResponse);
}

}
public void init(FilterConfig filterConfig) throws ServletException {
//throw new UnsupportedOperationException(“Not supported yet.”);
}
public void destroy() {
//throw new UnsupportedOperationException(“Not supported yet.”);
}

}

web.xml file

<xmlversion=”1.0″ encoding=”UTF-8″?>
<xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“>
<filter>
<filter-name>IPFilter</filter-name>
<filter-class>com.web.filterexample.IPFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>IPFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

index.jsp file

<%–
Document : index
Created on : May 6, 2011, 5:19:10 PM
Author : tunatore
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Calling Filter</title>
</head>
<body>
<h1>Calling Filter</h1>
<a href=”http://localhost:8084/FilterExample”>Call</a&gt;
</body>
</html>

Download WEB application source code(FilterExample)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s