问题
For a Java EE web application, I have a listener that implements ServletRequestListener, and a Filter.
Is there a way to specify at web.xml that the filter should be called before the listener?
I've already tried declaring the filter and its mapping before the listener, but the listener is still executed before.
Any idea?
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.example.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.example.MyServletRequestListener </listener-class>
</listener>
回答1:
When Browser(client) request to the Server , the container like (Tomcat) create the Request Object for the client request HttpServletRequest and Response Object HttpServletResponse and if you configure any listener which implements "ServletRequestListener" then "public void requestInitialized(ServletRequestEvent sre)" method will call
After creation of Request and Response Object by container if there is any listener for Request then Listener will execute first.....
After that HttpServletRequest and HttpServletResponse are assign to the Fillter , if you have configure the Fillter....
Means Listener come in picture first for ServletRequest . So there is no way to configure to make Fillter execute before Listener in ServletRequest case ....
回答2:
The ServletRequestListener.requestInitialized()
will be initialized before any filter is invoked and ServletRequestListener.requestDestroyed()
after all filter and service method returns.
来源:https://stackoverflow.com/questions/10191388/web-xml-order-or-filter-and-listener