web.xml order or filter and listener

霸气de小男生 提交于 2020-01-01 10:54:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!