Tuesday, January 1, 2013

Adding the executing order for the Servlet 3 Annotation based Filter classes

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.

then add the following two filters also. you can see that here we are using the annotated servlet filters. 
 FilterOne.java  
FilterTwo.java  

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