SERVLET FILTERS


Filters are powerful tools in servlet environment.It process request before it reaches a servlet and can process response before it leaves to a servlet.
You can map filters to a URL or a servlet name. When a filter is mapped to a URL (path-mapped), the filter applies to every servlet and JSP in the Web application. When a filter is mapped to a servlet name (servlet-mapped), it applies to a single servlet or JSP.
Filters may be used for large number of tasks like compressing response, modifying request and response headers, logging, change the content type, authenticating the user etc. For eg: suppose you want to check whether the user is already logged in or not. If you are using JSP, you have to write the authentication check in another JSP and include it in all JSP pages. Rather than including it in all pages, you may create an authentication filter class and attach it to all JSP pages. 
 Filters are configured in the deployment descriptor(web.xml) of a web application so that your JSP or Servlet will remain much clean containing code for specific purposes.

There are three interfaces defined in javax.servlet package.
  • FilterConfig – This object acts as configuration object for filter which pass information to filter during initialization. For eg, you may set initialization parameters in web.xml file for filter which could be accessed from this object. It also allow us to get a reference to ServletContext object. Every servlet container has its own implementation of FilterConfig interface.
  • FilterChain – This object is again created by servlet container which provides a convenient method to invoke next filter in the chain or final resource. The method is doFilter (ServletRequest request, ServletResponse response). Suppose one servlet is attached with two filters. In first filter calling doFilter() method of FilterChain object invokes second filter and calling the method in second filter invokes servlet itself.
  • Filter – This is the filter object which actually performs filtering tasks. For creating Filter you have to declare a class which implements this interface.
Servlet filters must implement the javax.servlet.Filter interface and define these methods:
 
Method Summary
void
destroy()
Called by the web container to indicate to a filter that it is being taken out of service.
 void
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
void
init(FilterConfig filterConfig)
Called by the web container to indicate to a filter that it is being placed into service.


In doFilter() method, the following steps are executed.
  • When a client makes HTTP request to a resource, the servlet container checks whether it is associated with filter.
  • If yes, the filter’s doFilter method is called.
  • Inside doFilter() method we may intercept request object and may modify the same. After that we call doFilter() method of FilterChain object. Here it invokes next filter in chain. If no more filters are available, final resource (Servlet, JSP etc) is called.
  • The resource like Servlet or JSP process the request and send response back to client. This again is passed through doFilter method of filter object. All statements written after doFilter() method of chain object is executed again.
  • Finally the response is sent back to client
The skeleton of doFilter() method is given below.:
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException{
      //preprocess statements for request/response objects
      ..................................................................
      ..................................................................     
      //call doFilter() of chain object, invoke next filter or resource     
      chain.doFilter (request, response);     
      //post-processing of request/response objects     
      ..................................................................
      ..................................................................
}




Comments

Popular posts from this blog

Servlet Advantages and Disadvantages

The Deployment Descriptor: web.xml

Session Management