Home  >  JSP & Servlet

How to Use Filter in Servlet 3 with @WebFilter Annotation

By Arvind Rai, January 14, 2015
@WebFilter Annotation has been introduced in Java EE 6. It defines different element like filterName, asyncSupported and servletNames etc. @WebFilter cannot be used without web.xml because @WebFilter does not define order. @WebFilter reduces the other configuration in web.xml.

@WebFilter

@WebFilter annotation declares a filter. The servlet container processes a filter at deployment time and associates to the specified URL, servlet and dispatcher. It does not defines order.

asyncSupported: It supports asynchronous operation in filter.
dispatcherTypes: It associates a dispatcher to a filter.
filterName: Defines a filter name.
initParams: Initialization parameter to the filter.
servletNames: Defines servlet to which the filter will be applied.
urlPatterns: Defines URL pattern.

@WebFilter Does Not Define the Order of Filter

@WebFilter has no element to define the order of filter of execution. We need to define order in web.xml. This may be because if we decide order of filter at class level using @WebFilter, then what if we include third party library which has filter with same order.


Example for @WebFilter with web.xml

Now find the example of @WebFilter. We are creating two filter and a servlet. The order of filter will be defined in web.xml. Find the filter one.
FilterOne.java
package com.concretepage;
import java.io.IOException;
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;
@WebFilter(filterName="filterOne")
public class FilterOne implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("Inside filter one.");
		chain.doFilter(request, response);
	}
	@Override
	public void destroy() {
	}
} 
Find the filter two.
FilterTwo.java
package com.concretepage;
import java.io.IOException;
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;
@WebFilter(filterName="filterTwo")
public class FilterTwo implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("Inside filter two.");
		chain.doFilter(request, response);
	}
	@Override
	public void destroy() {
	}
} 
Find the web.xml.
 <?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="3.0">
	<display-name>Filter Demo</display-name>
	<filter-mapping>
	    <filter-name>filterOne</filter-name>
	    <url-pattern>/msg/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
	    <filter-name>filterTwo</filter-name>
	    <url-pattern>/msg/*</url-pattern>
	</filter-mapping>
</web-app> 
Find the servlet.
HelloWorldServlet.java
package com.concretepage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/msg/HelloWorldServlet", loadOnStartup = 1)
public class HelloWorldServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
	     doGet(request,response);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
	     response.setContentType("text/html");
	     System.out.println("Inside servlet.");
	     PrintWriter out = response.getWriter();
	     out.println("Hello World!");
	}
} 

Output

To run the demo deploy the war file and access URL
http://localhost:8080/ServletDemo-1/msg/HelloWorldServlet
We will get output in console as below.
Inside filter one.
Inside filter two.
Inside servlet. 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us