Monday, July 18, 2016

Filters Vs Servlet

A filter is a web component on the web server that filters the request and response client and the business unit.
Filters are monitoring the request/response before it reaches its destination. So the filters are transparent to the client and Servlets
.
Filters Vs Servlet

Filters do not themselves create a response like servlet. It just modify header and information in the request/response before and after the servlet invocation. Filters are used for Logging and blocking the request-response. It is giving the customized version of the request and response.

Servlet is used to control the request and perform action on that particular request. Actions involves login, interaction with database and execution of business logic.

How filters are working:

Filters will process the request before sending to the servlet, and after processing the request filters can do the following:
  • ·        Filters can generate response and return it to the client.
  • ·        Filters will be able to modify the request and will send that modified/unmodified request to next filter (if there is filter changing) in the chain or to the correct resource.
  • ·        Filters can route the request to different resource other than the specified one.
  • ·        The response form the servlets are passing back to the client through the same set of filters (the request are send to servlet), but will be in reverse order.
  • ·        Filters will be able to modify the response before sending to the client.


How to use the filter:

A filter is a java class that implements javax.servlet.Filter

It defines three methods, these are declared in Filter Interface.

init()
doFilter()
destroy()

void init(FilterConfig config) throws ServletException
  • ·        It is used to initialize the filter.
  • ·        Called by the web container before the filter goes into service, and sets the filter's configuration object. It is invoked only once.

void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException:

  • ·        Called by the web container each time a request/response pass through the filter chain. Performs the actual filtering tasks

void destroy()

  • ·        Called by the web container after the filter has been taken out of service.
  • ·         It is called only once.


chain.doFilter() : To  transfer control to the next filter.

public class TimerLogFilter implements Filter  {  // Implements Filter interface

private FilterConfig filterConfig;  

   public void  init(FilterConfig config) throws ServletException  {

   this.filterConfig = filterConfig;                // creating filter config object
   }
   public void  doFilter(ServletRequest request, ServletResponse response,
                 FilterChain chain) throws java.io.IOException, ServletException {

     long start = System.currentTimeMillis();
    System.out.println("Milliseconds start: " + start);
    chain.doFilter(request, response);      //Pass request back down the filter chain
    long end = System.currentTimeMillis();
    System.out.println("Milliseconds end: " + end);
   }
   public void destroy( ){

      /* Called before the Filter instance is removed from service by the web container*/
                       filterConfig = null;

   }
Servlet Filter Mapping in Web.xml:

-- Mapping filter class to a filter name
<filter>
    <filter-name>timerLog</filter-name>  
    <filter-class>filter.TimerLogFilter </filter-class>
</filter>
-- Mapping a filter name to servlet name or URL pattern
-- Servlet name , to have the filter invoked whenever the servlet of name myservlet is invoked:
<filter-mapping>
    <filter-name> timerLog </filter-name>
    <servlet-name>myservlet</servlet-name>
</filter-mapping>
-- OR URL pattern ,invoke for all servlet.
<filter-mapping>
   <filter-name> timerLog </filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

No comments:

Post a Comment