Filter Servlet 3 using Webfilter Annotation Example

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

Filter examples
1. The filter applies for the URL pattern /user:

2. The filter applies for all URLs:

3. The filter applies for a specific servlet:

4. The filter applies for multiple servlets:

5. The filter is with the initialization parameters:

6. The filter is with the dispatcher types

7. Specify additional information for the filter:

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:

This filter can not itself register with container so we need to do that via web.xml

We create a simple servlet

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 Servlet 3 using Webfilter Annotation Example

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

The we need add this filter in web.xml. if this filter is added after the above filter it will run after that filter.

We run the URL http://localhost:8080/filter-servlet/filterDemo again. We will see their execution order like the screen shot.

Filter Servlet 3 using Webfilter Annotation Example

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 (58 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons