Well, straight to the point: i created - with Eclipse - two filters (Filter1 and Filter2) and a servlet(DisplayHeader) to see the filter order of execution. I used Tomcat 8.5 as target runtime.
New/Dynamic Web Project/ with Context root / and checked the "Generate web.xml delpoyment descriptor"
Filter1 prints "I'm Filter 1" when it's executed, and i "linked" it to a specific servlet "DisplayHeader". Filter2 it's the twin brother with 2 instead of 1. DisplayHeader servlet is mapped to /DisplayHeader.
Below is Filter1 code for 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 your code here
System.out.println("I'm Filter 1");
// pass the request along the filter chain
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
Well, when i run the servlet on Tomcat (Tomcat v8.5) it prints
I'm Filter 1
I'm Filter 2
which means that Filter1 is executed before Filter2.
I read that filter's order of execution comes from the mapping order in the web.xml file, so i expected to find somewhere in the web.xml file something 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 i should reverse in order...
The problem is: when i open the web.xml of my project (from the project explorer, see figure below) file i just 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 the explorer
am i just missing the right web.xml file path or this web.xml is the result of using the "Generate web.xml delpoyment descriptor" option?