eclipse - Filter order and web.xml content -


well, straight point: created - eclipse - 2 filters (filter1 , filter2) , servlet(displayheader) see filter order of execution. used tomcat 8.5 target runtime.

new/dynamic web project/ context root / , checked "generate web.xml delpoyment descriptor"

filter1 prints "i'm filter 1" when it's executed, , "linked" specific servlet "displayheader". filter2 it's twin brother 2 instead of 1. displayheader servlet mapped /displayheader.

below filter1 code clarity

import java.io.ioexception; import javax.servlet.*; import javax.servlet.annotation.webfilter;  @webfilter(         urlpatterns = {"/filter1"},          servletnames = {"displayheader"}         ) public class filter1 implements filter {      /**      * default constructor.       */     public filter1() {         // todo auto-generated constructor stub     }      /**      * @see filter#destroy()      */     public void destroy() {         // todo auto-generated method stub     }      /**      * @see filter#dofilter(servletrequest, servletresponse, filterchain)      */     public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {         // todo auto-generated method stub         // place code here         system.out.println("i'm filter 1");          // pass request along filter chain         chain.dofilter(request, response);     }      /**      * @see filter#init(filterconfig)      */     public void init(filterconfig fconfig) throws servletexception {         // todo auto-generated method stub     }  } 

well, when run servlet on tomcat (tomcat v8.5) prints

i'm filter 1 i'm filter 2 

which means filter1 executed before filter2.

i read filter's order of execution comes mapping order in web.xml file, expected find somewhere in web.xml file like

<filter-mapping> <filter-name>filter1</filter-name> <url-pattern>/displayheader</url-pattern> </filter-mapping>  <filter-mapping> <filter-name>filter2</filter-name> <url-pattern>/displayheader</url-pattern> </filter-mapping> 

which should reverse in order...

the problem is: when open web.xml of project (from project explorer, see figure below) file see this:

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1">   <display-name>setfilterorder</display-name>   <welcome-file-list>     <welcome-file>index.html</welcome-file>     <welcome-file>index.htm</welcome-file>     <welcome-file>index.jsp</welcome-file>     <welcome-file>default.html</welcome-file>     <welcome-file>default.htm</welcome-file>     <welcome-file>default.jsp</welcome-file>   </welcome-file-list> </web-app> 

web.xml location in explorer

am missing right web.xml file path or web.xml result of using "generate web.xml delpoyment descriptor" option?

from reading have done not servlet 3.0 provides way define order via annotations. define order have use web.xml. order may using alphabetical or dumb luck, classes show first when annotation scan. can test out messing around filter names or filter class names. cannot rely on this. seems have revert web.xml.

references:
how define servlet filter order of execution using annotations in war

http://www.concretepage.com/java-ee/jsp-servlet/how-to-use-filter-in-servlet-3-with-webfilter-annotation

http://javabycode.com/java-frameworks/servlet-jsp/filter-servlet-3-using-webfilter-annotation-example.html


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -