aspose file tools*
The moose likes Spring and the fly likes ContextLoaderListener in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "ContextLoaderListener in web.xml" Watch "ContextLoaderListener in web.xml" New topic

ContextLoaderListener in web.xml

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 207

Hi all,
I have following web.xml file

Why do i have duplicate entries in the context-param and init param for the dispatcher servlet.I am not having the xxx-servlet file which the dispatcher servlet searches by default.I have all the bean definitions in the two xml files given.So,why the duplicate entries?

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

I am going to answer both threads you have in this one post.

ContextLoaderListener is a ServletListener. So in the JSP/Servlet spec a Servlet Listener gets called by the container when certain events occur. In this case it gets called right after when the ServletContext is created for the web application. When it gets called, inside the ContextLoaderListener is code to create/instantiate an ApplicationContext and load in the xml file you set with the context-param name and value tags. In those xml files you define your middle tier beans only. No Web Tier beans should be in the xml file/files that is loaded by the ContextLoaderListener.

The DispatcherServlet in its init method also creates an ApplicationContext, but this one should load in only the Web Tier beans like your Controllers, ViewResolvers, MappingHandlers etc.

So the reason why you do this is to separate the web tier from the middle tier. The web tier beans can see the middle tier beans, but not the other way around.

In your example you got it wrong because you put the same xml config files to be loaded by both the ContextLoaderListener and the DispatcherServlet.

So my guess is that your /WEB-INF/mvc-config.xml should be in the DispatcherServlet config and the

repository-config.xml is what should be in the <context-param> with your ContextLoaderListener.

Hope that helps


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 207

Thanks Mark for the reply.
It really helped.
I want to have one more doubt get cleared.In the last xml file i posted,do I have two ApplicationContexts genearted then?One by the DispatcherServlet and other by ContextLoaderListener?Also,do i have all my beans created when my application context gets created/loaded?
Who is called first,DispatcherServlet or ContextLoaderListener?
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

All the beans you have configured in your xml have been created twice. You duplicated the xml files in both the ContextLoaderListener and DispatcherServlet web.xml configuration, but one instance of each bean in each application context. You get two ApplicationContexts created.

In JSP/Servlets, what gets created first the ServletContext or a Servlet?

That answers which application context gets created first. DispatcherServlet is a Servlet. ContextLoaderListener gets called right after the ServletContext gets created.
Dakshinamurthy Babu

Joined: Oct 17, 2012
Posts: 1
I am doing a sample springs extjs application. In that i have a text box and submit button in front end. And i used the handler function as

handler : function()

var user=Ext.getCmp('username').getValue();
url : '/'

and my controller is

public class HelloWorldController {

@RequestMapping(value = "/hi", method = RequestMethod.POST)
public void display(@RequestParam("username") String name, Model model) {
String message = "Hi " + name + "!";


My web.xml is



But i couldnt getting that textbox value in controller.

Am new to springs.. SO please help me. Thanks in advance....

I agree. Here's the link:
subject: ContextLoaderListener in web.xml
Similar Threads
JSF+Spring+Hibernate Session Problem
how to initiate hashtable and linklist
Application context in spring
Problem with RichFaces
how to set domain namin on jboss as 4.0.5