Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.xml order or filter and listener

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>
like image 628
David Portabella Avatar asked Apr 17 '12 12:04

David Portabella


People also ask

How do I order filters in web xml?

xml, the order of the chain is determined by the order in which matching filter mappings appear in the web. xml file, with <url-pattern> matches taking precedence over <servlet-name> matches. This is contrary to the way in which servlet URL matching is done, with specific matches taking the highest priority.

What is the difference between listener and filter?

In short, Filter is for the Servlet, intercepting the requests and responses. Listener is for the Web Application, doing important tasks on events in context-level, session-level etc.

What is filter and filter mapping in web xml?

The filter-mapping element maps a URL pattern or servlet name to an instance of a filter. The filter-mapping always contains a filter-name element and a url-pattern element. The filter-name element must match a filter-name defined in a filter element elsewhere in the web. xml file.

Which of the following listener is used to track number of active sessions or users?

Here's a simple “HttpSessionListener” example to keep track the total number of active sessions in a web application. If you want to keep monitor your session's create and remove behavior, then consider this listener.


2 Answers

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 ....

like image 179
Hitesh Avatar answered Jan 03 '23 18:01

Hitesh


The ServletRequestListener.requestInitialized() will be initialized before any filter is invoked and ServletRequestListener.requestDestroyed() after all filter and service method returns.

like image 36
Ramesh PVK Avatar answered Jan 03 '23 19:01

Ramesh PVK