The tutorial shows you how to create a Filter Servlet 3 using Webfilter Annotation Example. @WebFilter Annotation has been introduced in Java EE 6. It defines different attribute like filterName, asyncSupported and servletNames etc. If we use @WebFilter annotation we will reduce the web xml configuration. It means that we have to register filter name with web.xml. This is twelfth post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.
Table of contents
1. @WebFilter introduction
2. Filter Syntax
3. Filter examples
4. Demonstrate a completely filter example
5. Filter Order
@WebFilter introduction
This annotation is processed by the container at deployment time, and the corresponding filter applied to the specified URL patterns, servlets, and dispatcher types. It does not defines order. It defines attributes like these:
value or urlPatterns: Specify one or more URL patterns to which the filter applies.
asyncSupported: It supports asynchronous operation in filter.
dispatcherTypes: It associates a dispatcher to a filter.
filterName: Defines a filter name.
displayName: Display name of the filter.
description: Description of the filter.
initParams: Initialization parameter to the filter.
servletNames: Defines servlet to which the filter will be applied.
smallIcon: Specify name of the small icon of the filter.
largeIcon: Specify name of the large icon of the filter.
Filter Syntax
1 2 3 4 5 6 7 8 |
@WebFilter( attribute1=value1, attribute2=value2, ... ) public class CustomFilter implements javax.servlet.Filter { // implements Filter's methods } |
Filter examples
1. The filter applies for the URL pattern /user:
1 2 3 |
public class CustomFilter implements Filter { // do something... } |
2. The filter applies for all URLs:
1 2 3 4 |
@WebFilter("/*") public class CustomFilter implements Filter { // do something... } |
3. The filter applies for a specific servlet:
1 2 3 4 |
@WebFilter(servletNames = "MyServlet") public class CustomFilter implements Filter { // do something... } |
4. The filter applies for multiple servlets:
1 2 3 4 |
@WebFilter(servletNames = {"MyServlet", "UploadFileServlet"}) public class CustomFilter implements Filter { // do something... } |
5. The filter is with the initialization parameters:
1 2 3 4 5 6 7 |
@WebFilter( urlPatterns = "/upload", initParams = @WebInitParam(name = "fileTypes", value = "png;jpg;gif") ) public class CustomFilter implements Filter { // do something... } |
6. The filter is with the dispatcher types
1 2 3 4 5 6 7 |
@WebFilter( urlPatterns = "/user", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD} ) public class CustomFilter implements Filter { // do something... } |
7. Specify additional information for the filter:
1 2 3 4 5 6 7 8 |
@WebFilter( urlPatterns = "/user/*", filterName = "UserFilter", description = "Filter all user URLs" ) public class CustomFilter implements Filter { // do something... } |
Demonstrate a completely filter example
Now, we are ready to create the Filter Servlet 3 using Webfilter Annotation Example. This example includes a filter and a simple servlet example to show how a filter works. We declare a filter such as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javabycode.filter; 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 = "customFilter") public class CustomFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("-------- Do something in Customer Filter --------"); chain.doFilter(request, response); } public void destroy() { } } |
This filter can not itself register with container so we need to do that via web.xml
1 2 3 4 5 6 7 8 9 10 11 |
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Filter Demo</display-name> <filter-mapping> <filter-name>customFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
We create a simple servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javabycode.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/filterDemo") public class FilterServletDemo extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter prinOut = response.getWriter(); prinOut.write("Hello, This is filter demo"); } } |
It’s time to deploy our application on container (for example, Tomcat 8). We run the URL http://localhost:8080/filter-servlet/filterDemo. And you will see the message which is printed by our filter immediately.
Filter Order
@WebFilter has no element to define the order of filter of execution. We need to define order in web.xml. We create one more filter like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javabycode.filter; 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 = "customFilterTwo") public class CustomFilterTwo implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("-------- Do something in Customer Filter Two --------"); chain.doFilter(request, response); } public void destroy() { } } |
The we need add this filter in web.xml. if this filter is added after the above filter it will run after that filter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Filter Demo</display-name> <filter-mapping> <filter-name>customFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name>Filter Demo Two</display-name> <filter-mapping> <filter-name>customFilterTwo</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
We run the URL http://localhost:8080/filter-servlet/filterDemo again. We will see their execution order like the screen shot.
That’s it on how to create the Filter Servlet 3 using Webfilter Annotation Example. You can also find other servlet example in the series of Java Servlet Example. If you have any opinion, please leave comment. Thank you so much.
You can download source code of the Filter Servlet 3 using Webfilter Annotation Example to study and deploy it on your environment.
Download complete source code, please click link below
Servlet-3-Webfilter-Annotation.zip (325 downloads)