@WebFilter

This section contains detailed description of @WebFilter annotation and its implementation with sample code.

@WebFilter

@WebFilter

This section contains detailed description of @WebFilter annotation and its implementation with sample code.

Servlet filters are added in the Servlet 2.3. The main task of the Filters is to intercept and modify  server's requests and response. Prior to Servlet 3.0, you need to register filter using deployment descriptor(web.xml). You can do this as follows :

<filter>
	<filter-name>MyFilter</filter-name>
	<filter-class>
		net.roseindia.filters.MyFilter
	</filter-class>
	<init-param>
		<param-name>param1</param-name>
		<param-value>roseindia</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>MyFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

The annotation @WebFilter, added in Servlet 3.0, is used to register a filter in web application. The attributes of the annotation @WebFilter are given below :

Attribute

Desciption

asyncSupported It tells us whether a Filter supports asynchronous mode or not. Its value is either true or false.
description It contains the description of the Filter.
dispatcherTypes It contains the types of dispatchers which applies to the filter.
displayName It contains the filter's display name.
filterName It contains the name of the filter.
initParams Contains the init parameters of the filters.
largeIcon  Contains the URL of the large icon.
servletNames Contains the name of the Servlets to which this filter applies.
smallIcon Contains the URL of the filter's small icon.
urlPatterns It contains the URL patterns to which this filter applies. You can use only one, either urlPatterns  or value attribute.
value It contains the URL patterns to which this filter applies. You can use only one, either urlPatterns  or value attribute.

The tools and technology used in the example is given below :

  • Eclipse Helios 3.6.1

  • Tomcat 7

  • jdk1.6.0_18

EXAMPLE

In the below example, you will learn how to register the filter using  @WebFilter :

package roseindia;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;

@WebFilter(filterName = "filteranno", urlPatterns = "/*",
 initParams = { @WebInitParam(name = "param1", value = "RoseIndia") })
public class WebFilterAnnotation implements Filter {
	public void doFilter(ServletRequest req, ServletResponse res,
		FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		
		// 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());
		
		chain.doFilter(req, res);
		}
		
		public void init(FilterConfig config) throws ServletException {
		
		// Get init parameter
		String testParam = config.getInitParameter("param1");
		
		// Print the init parameter
		System.out.println("Init Param Name : " + testParam);
		}
		
		public void destroy() {
		// add code to release any resource
	}
}

OUTPUT

After your application starts executing by Server, any request made will display the message like this on console :

Download Source Code