07 Filter

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Deccansoft Software Services Adv Java / Filter

------------------------------------------------------------------------------------------------------------------------------------------
A web request has to go through various conditions which determine whether the request can be processed or not.
These conditions have to be implemented programmatically. It is advisable to put such code out side the
component so that it can be reused and in case of any change, we need not touch the actual end target of the
request. This makes the code more manageable.

The pre and post processing components are implemented through Filter concept. There can be many filters thus
forming a Filter Chain.

Note: The earlier name of such concept was Servlet Chain since the individual components were Servlets. From
version 2.3 of Servlets a new concept called Filter Concept is implemented for implementing such pattern.

The following interface is implemented by the Filter Component:


javax.servlet.Filter

This interface has 3 methods:


public void init(FilterConfig config) throws ServletException
In the life time of the filter this method gets invoked only once. We can perform instance level initializations in this
method.

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)


throws ServletException, IOException
{
//pre-processing logic here
chain.doFilter (request, response)
//post-processing logic here
}
This method contains the logic of writing the filter. The Chain allows the request move ahead to the current filter to
the next filter.

chain.doFilter(): will result in next component in the chain to be invoked. In the life time of a Filter this method
will invoke more than once depending on the URL pattern of the request.

public void destroy()


Will be the last method to be invoked on the filter object before the container destroys the instance.

Note: The Web Container maintains a single instance of each filter in the application and all the requests will end
up the same instance.

Some of the popular usages of Filter concepts are:


1. Logging Purpose: Logging implies pulling the information about the access of the server resources in the files.
If logging is required for more than one target component then filter is an appropriate place for logging.
2. Authentications Filters: Such filters would check whether the request is coming from an authenticated client
and users fall in a role for which there is permission available to access the component. Logging Filters are
ideal candidates for such purpose.
3. Transformation Filters: The web can be accessed from variety of clients e.g. a normal desktop browser
understands HTML where as handheld devices understand WML (Wireless Markup Language), CHTML
(Compact HTML). Instead of writing one JSP for individual content type we can write a filter which will
transform the same XML in to various content types.
Note: We can find out the kind of browser used by the client since such information is present in request header
whose name is HTTP_USERAGENT.

Steps for Writing a Filter:


1. Identify the purpose of writing a Filter and implement the code in the Filter Interface class.
2. Compile the filter and place the class in the classes’ folder or if the class is in the jar file then place it in the lib
folder.
3. Provide an appropriate URL Pattern Mapping for the filter.
4. Test the Filter.
1
Deccansoft Software Services Adv Java / Filter
------------------------------------------------------------------------------------------------------------------------------------------
The TimeFilter class displays the time taken for the request to go ahead and comeback through the filter.
--------------------------------------------------------TimeFilter.java--------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TimeFilter implements Filter


{
private FilterConfig config;

public void init( FilterConfig config ) throws javax.servlet.ServletException


{
this.config = config;
}

public void doFilter( ServletRequest request , ServletResponse response , FilterChain chain)


throws IOException , ServletException
{
long t1 = System.currentTimeMillis();
chain.doFilter(request,response);
long t2 = System.currentTimeMillis();
HttpServletRequest httpRequest = ( HttpServletRequest ) request;
String myURI = httpRequest.getRequestURI();
System.out.println(myURI + ":" + (t2-t1) );
}

public void destroy()


{
config = null;
}
}
--------------------------------------------------------/WEB-INF/web.xml---------------------------------------------------------
<web-app>
<filter>
<filter-name> TimeFilter</filter-name>
<filter-class>TimeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> TimeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
------------------------------------------------------------------------------------------------------------------------------------------
Directory Structure of web application:
Web App Folder
|
| --> welcome.html
| --> first.jsp
| --> WEB-INF
| --> web.xml
| --> classes
| --> TimeFilter.class

The Filter Chain is constructed on the following basis:

The more generic filter comes first and then the specific filter. If 2 filters are at the same level, then the order of
declaration will be used for constructing the chain.

You might also like