Sunday, March 1, 2009

Servlet Filters

Servlet filters are a new addition to the Servlet 2.3 specification that's in its public final draft stage, released in October, 2000. Filters are Java classes that can intercept requests from a client before they access a resource; manipulate requests from clients; intercept responses from resources before they are sent back to the client; and manipulate responses before they are sent to the client.

Link: Writing a Simple Filter
Link: Deploying Filters

Code Snippets
Listing 1: An example of a very simple filter (SimpleFilter.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 SimpleFilter implements Filter
{
private FilterConfig filterConfig;

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

try
{
System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Request ...");

chain.doFilter (request, response);

System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Response ...");

} catch (IOException io) {
System.out.println ("IOException raised in SimpleFilter");
} catch (ServletException se) {
System.out.println ("ServletException raised in SimpleFilter");
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig (FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
}



Link: Link to Complete Article

Others:

Link: Sun Microsystem Article on Basic of Filters

No comments:

Post a Comment