Juns
Juns

Reputation: 521

Difference between ContextLoaderListener and RequestContextListener?

I have googled it but not found satisfactory answer, it would be great if you guys can explain the difference between ContextLoaderListener and RequestContextListener.

Upvotes: 27

Views: 18273

Answers (4)

SRK
SRK

Reputation: 158

If you use a Servlet 2.5 web container, with requests processed outside of Spring’s DispatcherServlet (for example, when using JSF or Struts), you need to register the org.springframework.web.context.request.RequestContextListener ServletRequestListener. For Servlet 3.0+, this can be done programmatically via the WebApplicationInitializer interface. Alternatively, or for older containers, add the following declaration to your web application’s web.xml file:

    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>

</web-app>

Upvotes: 5

AlphaBetaGamma
AlphaBetaGamma

Reputation: 1

ContextLoaderListener is a bootstrap listener to startup Spring's root WebApplicationContext.

RequestContextListener is used when you want the attributes in your request thread to stay alive.

The point to be noted here is that, the inheritable flag is set to false in the RequestContextListener.

http://docs.spring.io/spring/docs/4.0.6.RELEASE/javadoc-api/org/springframework/web/context/request/RequestContextListener.html

So If you want that a child thread inherits the request attributes then you should try using RequestContextFilter or RequestContextHolder.

http://docs.spring.io/spring/docs/4.0.6.RELEASE/javadoc-api/org/springframework/web/filter/RequestContextFilter.html

Upvotes: 0

Mikhail
Mikhail

Reputation: 2929

I have read that If you use the ContextLoaderListener you don't need the RequestContextListener or Filter. It registers the current request(attributes) in a thread local so that the scoped proxies can use it.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

contextloaderlistener :- Bootstrap listener to start up Spring's root WebApplicationContext. Simply delegates to ContextLoader.

requestcontextlistener :- This listener is mainly for use with third-party servlets, e.g. the JSF FacesServlet. Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.

Upvotes: 16

Related Questions