JSP Tutorial - JSP Filters








JSP Filters are Java classes that can be used for intercepting requests from a client or manipulating responses from server.

The filters can be used to do Authentication, Encryption, Logging, Auditing.

We can map filters to either JSP names or URL patterns in application's deployment descriptor file web.xml.

The filters execute in the order that they are declared in the deployment descriptor.

A filter is a Java class that implements the javax.servlet.Filter interface.

The javax.servlet.Filter interface defines three methods:

MethodDescription
doFilter (ServletRequest, ServletResponse, FilterChain)called by the container when a request/response pair is passed through the chain.
init(FilterConfig filterConfig)indicate to a filter being placed into service.
void destroy()indicate to a filter that it is being taken out of service.




Example

The following code shows how to create a JSP Filter that prints the IP address.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
 
public class LogFilter implements Filter  {
   public void  init(FilterConfig config) 
                         throws ServletException{
      String testParam = config.getInitParameter("test-param"); 
      System.out.println("init");
   }
   public void  doFilter(ServletRequest request, 
                 ServletResponse response,
                 FilterChain chain) 
                 throws java.io.IOException, ServletException {
 
      String ipAddress = request.getRemoteAddr();
      System.out.println("IP "+ ipAddress);
 
      chain.doFilter(request,response);
   }
   public void destroy( ){
      System.out.println("destroy");
   }
}

Compile LogFilter.java and put the LogFilter.class class file in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.

Create the following entry for filter tag in the deployment descriptor file web.xml

<filter>
   <filter-name>LogFilter</filter-name>
   <filter-class>LogFilter</filter-class>
   <init-param>
    <param-name>your-param</param-name>
    <param-value>Initialization Parameter</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

The above filter would apply to all the JSP because we specified /* in our configuration.





Multiple Filters

The order of filter-mapping elements in web.xml determines the order in which the web container applies the filter to the servlet or JSP.

The following code shows how to use two filters in web.xml file.

<filter>
   <filter-name>LogFilter</filter-name>
   <filter-class>LogFilter</filter-class>
   <init-param>
    <param-name>test-param</param-name>
    <param-value>Initialization Parameter</param-value>
   </init-param>
</filter>
 
<filter>
   <filter-name>MyFilter</filter-name>
   <filter-class>MyFilter</filter-class>
   <init-param>
    <param-name>test-param</param-name>
    <param-value>Initialization Parameter</param-value>
   </init-param>
</filter>
 
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter-mapping>
   <filter-name>MyFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

To reverse the filter order from the above web.xml, use the following:

<filter-mapping>
   <filter-name>MyFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter-mapping>
   <filter-name>LogFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>