Servlets - Writing Filters


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

  • To capture demands from a customer before they get to an asset at back end.

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

There are different kinds of channels 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 descriptor record web.xml and after that guide to either servlet names or URL designs in your application's organization descriptor.

When the web holder begins up your web application, it makes an example of each channel that you have pronounced in the arrangement descriptor. The channels execute in the request that they are announced in the arrangement descriptor.

Servlet Filter Methods

A channel 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 technique is called by the compartment each time a demand/reaction pair is gone through the chain because of a customer ask for an asset toward the finish of the chain.

2

public void init(FilterConfig filterConfig)

This technique is called by the web holder to show to a channel that it is being set into service.

3

public void destroy()

This strategy is called by the web holder to demonstrate to a channel that it is being removed from service.

Servlet Filter − Example

Following is the Servlet Filter Example that would print the customers IP address and current date time. This model would give you essential comprehension of Servlet Filter, yet you can compose increasingly refined channel 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 customer 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 channel 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 channel tag in the arrangement descriptor record web.xml

<filter> 

<filter-name>LogFilter</channel name> 

<filter-class>LogFilter</channel class> 

<init-param> 

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

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

</init-param> 

</filter> 

<filter-mapping> 

<filter-name>LogFilter</channel name> 

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

</channel mapping>

The above channel 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 channel 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 lumberjack to log above sign in a different file.

Using Multiple Filters

Your web application may characterize a few distinct channels with a particular reason. Think of you as, characterize two channels 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</channel name> 

<filter-class>LogFilter</channel class> 

<init-param> 

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

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

</init-param> 

</filter> 

<filter> 

<filter-name>AuthenFilter</channel name> 

<filter-class>AuthenFilter</channel class> 

<init-param> 

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

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

</init-param> 

</filter> 

<filter-mapping> 

<filter-name>LogFilter</channel name> 

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

</channel mapping> 

<filter-mapping> 

<filter-name>AuthenFilter</channel name> 

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

</channel mapping>

Filters Application Order

The request of channel mapping components in web.xml decides the request in which the web compartment applies the channel to the servlet. To turn around the request of the channel, you simply need to invert the channel 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</channel name> 

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

</channel mapping> 

<filter-mapping> 

<filter-name>LogFilter</channel name> 

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

</channel mapping>






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

Python 3