Servlets - Writing Filters


Servlet Filters are Java classes that can be utilized in Servlet Programming for the accompanying purposes −

  • To capture requests from a client before they get to an asset at back end.

  • To control responses from server before they are sent back to the client.

There are different kinds of filters proposed by the details −

  • Authentication Filters.
  • Data pressure Filters.
  • Encryption Filters.
  • Filters that trigger asset get to events.
  • Image Conversion Filters.
  • Logging and Auditing Filters.
  • MIME-TYPE Chain Filters.
  • Tokenizing Filters .
  • XSL/T Filters That Transform XML Content.

Filters are sent in the sending deployment record web.xml and after that guide to either servlet names or URL designs in your application's organization descriptor.

When the web container begins up your web application, it makes an example of each filter that you have pronounced in the deployment descriptor. The filters execute in the request that they are announced in the deployment descriptor.

Servlet Filter Methods

A filter is just a Java class that executes the javax.servlet.Filter interface. The javax.servlet.Filter interface characterizes three techniques −

Sr.No. Method & Description
1

public void doFilter (ServletRequest, ServletResponse, FilterChain)

This method is called by the container each time a request/response pair is gone through the chain because of a client ask for an asset toward the finish of the chain.

2

public void init(FilterConfig filterConfig)

This method is called by the web container to show to a filter that it is being set into service.

3

public void destroy()

This method is called by the web container to demonstrate to a filter that it is being removed from service.

Servlet Filter − Example

Following is the Servlet Filter Example that would print the client s IP address and current date time. This model would give you essential comprehension of Servlet Filter, yet you can compose increasingly refined filter applications utilizing a similar idea −

//Import required java libraries 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.*; 

//Implements Filter class 

public class LogFilter implements Filter { 

public void init(FilterConfig config) throws ServletException { 

//Get init parameter  

String testParam = config.getInitParameter("test-param"); 

//Print the init parameter  

System.out.println("Test Param: " + testParam); 

} 

public void doFilter(ServletRequest request, ServletResponse response, 

FilterChain chain) throws java.io.IOException, ServletException { 

//Get the IP address of client  machine. 

String ipAddress = request.getRemoteAddr(); 

//Log the IP address and current timestamp. 

System.out.println("IP "+ ipAddress + ", Time " + new Date().toString()); 

//Pass ask for withdraw the filter chain 

chain.doFilter(request,response); 

} 

public void destroy( ) { 

/* Called before the Filter occasion is expelled from administration by the web container*/ 

} 

}

Compile LogFilter.java in common way and put your class document in <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes

Servlet Filter Mapping in Web.xml

Filters are characterized and afterward mapped to a URL or Servlet, similarly as Servlet is characterized and afterward mapped to a URL design. Make the accompanying section for filter tag in the deployment deployment record web.xml

<filter> 

<filter-name>LogFilter</filter name> 

<filter-class>LogFilter</filter class> 

<init-param> 

<param-name>test-param</param-name> 

<param-value>Initialization Paramter</param-value> 

</init-param> 

</filter> 

<filter-mapping> 

<filter-name>LogFilter</filter name> 

<url-pattern>/*</url-pattern> 

</filter mapping>

The above filter would apply to all the servlets in light of the fact that we determined /* in our design. You can specicy a specific servlet way in the event that you need to apply filter on few servlets only.

Now attempt to call any servlet in common way and you would see produced sign in your web server log. You can utilize Log4J logger to log above sign in a different file.

Using Multiple Filters

Your web application may characterize a few distinct filters with a particular reason. Think of you as, characterize two filters AuthenFilter and LogFilter. Rest of the procedure would stay as clarified above aside from you have to make an alternate mapping as referenced beneath −

<filter> 

<filter-name>LogFilter</filter name> 

<filter-class>LogFilter</filter class> 

<init-param> 

<param-name>test-param</param-name> 

<param-value>Initialization Paramter</param-value> 

</init-param> 

</filter> 

<filter> 

<filter-name>AuthenFilter</filter name> 

<filter-class>AuthenFilter</filter class> 

<init-param> 

<param-name>test-param</param-name> 

<param-value>Initialization Paramter</param-value> 

</init-param> 

</filter> 

<filter-mapping> 

<filter-name>LogFilter</filter name> 

<url-pattern>/*</url-pattern> 

</filter mapping> 

<filter-mapping> 

<filter-name>AuthenFilter</filter name> 

<url-pattern>/*</url-pattern> 

</filter mapping>

Filters Application Order

The request of filter mapping components in web.xml decides the request in which the web container applies the filter to the servlet. To turn around the request of the channel, you simply need to invert the filter mapping components in the web.xml file.

For model, above precedent would apply LogFilter first and afterward it would apply AuthenFilter to any servlet however the accompanying model would turn around the request −

<filter-mapping> 

<filter-name>AuthenFilter</filter name> 

<url-pattern>/*</url-pattern> 

</filter mapping> 

<filter-mapping> 

<filter-name>LogFilter</filter name> 

<url-pattern>/*</url-pattern> 

</filter mapping>






© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3