welcome to my first post o the year 2013 ;) and i today decided to investigate through servlet 3.0 features. as you all are aware servlet 3 has a nice feature called annotations. therefore we can minimize the XML based declarations on the web.xml deployment descriptor. today i am going to show a simple demonstration on annotated servlet filters and how to add execution order for those servlet filters.
you can generate simple web application project with following maven command.
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
one the project is generate, make sure to check and alter the web application deployment descriptor based on the servlet 3 API version . i have post a article about deployment descriptors(web.xml) for different servlet versions and you can refer that article if you dont know how to do that and what is the purpose of doing that.
after creating the project, add following servlet3 maven dependency for the pom.xml of your project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>6.0</version> | |
<scope>provided</scope> | |
</dependency> |
FilterOne.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.web.filter; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.servlet.*; | |
import javax.servlet.annotation.WebFilter; | |
import java.io.IOException; | |
/** | |
* <p> | |
* filter execution order has been defined in the web.xml file | |
* </p> | |
*/ | |
@WebFilter( | |
filterName = "helloFilterOne", | |
urlPatterns = {"/hello"} | |
) | |
public class FilterOne implements Filter{ | |
private FilterConfig filterConfig; | |
private static final Logger logger = LoggerFactory.getLogger(FilterOne.class); | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
this.filterConfig = filterConfig; | |
} | |
@Override | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | |
logger.info(" inside the filter one "); | |
filterChain.doFilter(servletRequest,servletResponse); | |
} | |
@Override | |
public void destroy() { | |
//releasing the resources | |
} | |
} |
FilterTwo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.web.filter; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.servlet.*; | |
import javax.servlet.annotation.WebFilter; | |
import java.io.IOException; | |
/** | |
* <p> | |
* you can see that the filter name and url patters are defined with annotations. | |
* </p> | |
*/ | |
@WebFilter( | |
filterName = "helloFilterTwo", | |
urlPatterns = {"/hello"} | |
) | |
public class FilterTwo implements Filter { | |
private FilterConfig filterConfig; | |
private static final Logger logger = LoggerFactory.getLogger(FilterTwo.class); | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
this.filterConfig = filterConfig; | |
} | |
@Override | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | |
logger.info(" inside the filter two "); | |
filterChain.doFilter(servletRequest,servletResponse); | |
} | |
@Override | |
public void destroy() { | |
//releasing the resources | |
} | |
} |
if you need you can add the following servlet class also.
HelloServlet.java
you can deploy the web application in the tomcat and access the web application as follows.
http://localhost:8080/sample-servlet3-webapp/hello
then you will get the following result(check the application loggers).
inside the filter one
inside the filter two
this servlet will be executed after executing the all relevant filters according to their order
you can get the full source code from my GitHub account
Download Source code through GitHub