Writing Servlet 2.3 Filters
Pages: 1, 2, 3, 4
The filters in a web application are defined in the deployment
descriptor of the web application, the web.xml
file. Filters are defined and then mapped to a URL or Servlet, in much
the same was as Servlet is defined and then mapped to a URL
pattern. Listing 2 shows the web.xml file that deployed
the SimpleFilter.java from Listing 1, mapping it to a
Servlet.
Listing 2: The deployment descriptor for the simple filter. (web.xml)
<?xml version = "1.0" encoding = "ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<!-- Define the filters within the Web Application -->
<filter>
<filter-name>
Simple Filter Example
</filter-name>
<filter-class>
com.filters.SimpleFilter
</filter-class>
</filter>
<!-- Map the filter to a Servlet or URL -->
<filter-mapping>
<filter-name>
Simple Filter Example
</filter-name>
<url-pattern>
/simple
</url-pattern>
</filter-mapping>
<!-- Define the Servlets within the Web Application -->
<servlet>
<servlet-name>
Simple Servlet
</servlet-name>
<servlet-class>
com.servlets.SimpleServlet
</servlet-class>
</servlet>
<!-- Define Servlet mappings to urls -->
<servlet-mapping>
<servlet-name>
Simple Servlet
</servlet-name>
<url-pattern>
/simple
</url-pattern>
</servlet-mapping>
</web-app>
Figure 2 shows the console of the Tomcat server where this web application was running after the servlet had handled a request.
|
Filter Chaining
In the first example only one filter that handled the request and response for the simple servlet. What if you want multiple filters to handle the request and response? Figure 3 shows what a filter chain looks like.
|
Multiple filters can be written and applied to the same URL
pattern. The order of execution is determined by the ordering in the
deployment descriptor. Remember that in the doFilter()
method you call the next element in the chain with
chain.doFilter(request, response)
|
Listing 3 shows the code for Filter1.java, which is a
simple filter that outputs to the console of the web server a
message. The message allows you to track the request through the
filters.
Listing 3: A simple filter that outputs a message to the console. (Filter1.java)
package com.filters;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
public class Filter1 implements Filter
{
private FilterConfig filterConfig;
public void doFilter (ServletRequest request,
ServletResponse response,
FilterChain chain)
{
try
{
System.out.print ("Within First Filter ... ");
System.out.println ("Filtering the Request ...");
chain.doFilter (request, response);
System.out.print ("Within First Filter ... ");
System.out.println ("Filtering the Response ...");
} catch (IOException io) {
System.out.println ("IOException raised in Filter1 Filter");
} catch (ServletException se) {
System.out.println ("ServletException raised in Filter1 Filter");
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig (FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
}
The second filter prints a message to the console of the web server to help us track the request and response. Listing 4 shows the code for the second filter in the chain.
Listing 4. A simple filter that outputs a message to the console. (Filter2.java)
package com.filters;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
public class Filter2 implements Filter
{
private FilterConfig filterConfig;
public void doFilter (ServletRequest request,
ServletResponse response,
FilterChain chain)
{
try
{
System.out.print ("Within Second Filter ... ");
System.out.println ("Filtering the Request ...");
chain.doFilter (request, response);
System.out.print ("Within Second Filter ... ");
System.out.println ("Filtering the Response ...");
} catch (IOException io) {
System.out.println ("IOException raised in Filter2 Filter");
} catch (ServletException se) {
System.out.println ("ServletException raised in Filter2 Filter");
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig (FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
}


