ServletFilters

Posted on October 24, 2012. Filed under: SANS Dev 541 |


The java servlet specification version 2.3 introduces the new component type called Filter. The filter intercepts requests and responses to transform the content in them. The filter do not of themselves create response but instead provides functions that can be attached to any type of servlet or JSP pages.

The filter API is defined by the Filter, FilterChain, and FilterConfig interfaces in the javax.servlet package. You define a filter by implementing the Filter interface. A filter chain, passed to a filter by the container, provides a mechanism for invoking a series of filters. A filter config contains initialization data.

The most important method in the Filter interface is the doFilter method, which is the heart of the filter. In doFilter method, we can examines the request headers, modify request headers or data, invoke the next entity in the filter chain. It invokes the next entity by calling the doFilter method on the chain object.

For example, Currently, many browsers do not send character encoding information in the Content-Type header of an HTTP request. If an encoding has not been specified by the client request, the container uses a default encoding to parse request parameters. If the client hasn’t set character encoding and the request parameters are encoded with a different encoding than the default, the parameters will be parsed incorrectly. You can use the method setCharacterEncoding in the ServletRequest interface to set the encoding.

-CharSetFilter.java —

 import javax.servlet.*;

public class CharSetFilter implements Filter {

private FilterConfig filterConfig = null;

public void doFilter(ServletRequest request,
 ServletResponse response, FilterChain chain) throws
 IOException, ServletException {
 String encoding = selectEncoding(request);
 if (encoding != null)
 request.setCharacterEncoding(encoding);
 chain.doFilter(request, response);
 }
 public void init(FilterConfig filterConfig) throws
 ServletException {
 this.filterConfig = filterConfig;
 this.encoding = filterConfig.getInitParameter("encoding");
 }

public void destroy() {
 this.filterConfig = null;
 }

protected String selectEncoding(ServletRequest request) {
 return (this.encoding);
 }

}

-WEB.xml related changes—


<filter>
 <filter-name>charsetfilter</filter-name>
 <filter-class>net.CharSetFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>charsetfilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

The following is a video tutorial about ServletFilters.

[youtube http://www.youtube.com/watch?v=U5fXA9BRtTs]
Advertisements

Make a Comment

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

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: