• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

DispatcherServlet vs ContextLoaderListener?

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

In spring mvc,i have defined in web.xml DispatcherServlet and ContextLoaderListener.


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/xxxxxxx-servlet.xml
</param-value>
</context-param>


----------------------------------

<servlet>
<servlet-name>xxxxxxx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

---------------------------------------
How many Instances are created during the start up in container (service,dao,handler)?

Thanks,
SR
 
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one instance of dispatcher servlet which than traps all the request.

Again there is no reason to have more than one Context loader listener.
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..Lalit Bhatt..

I have very basic question. I understood in third party serlvets(DWR,STRUST...ETC) are defining in ContextLoaderListener only. But in spring MVC how to define in web.xml? Is it necessary to use both configurations? If i am doing both how can I avoid 2 instance creation in spring mvc?

Thanks,
selva
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks....

I have very basic question. I understood in third party serlvets(DWR,STRUTS..etc) are defining in ContextLoaderListener only. But in spring MVC how to define in web.xml? Is it necessary to use both configurations? If i am doing both how can I avoid 2 instance creation in spring mvc?

Thanks,
selva
 
Lalit Bhatt
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Spring MVC, there are two context. One is the application context or global context which is booted up by ContextLoaderListener. It takes all the configuration file mentioned in contextConfigLocation parameter.

Now if you are are using Spring MVC also than Dispatcher servlet is required, which boots up another container which is also known as web application container. This container takes the global container as a parent.

If you are using Spring in the backend only, then the global context is good enough. However if you are using Spring MVC also for front end than you need Dispatcher Servlet also.
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to configure third part framework (I.example : DWR,STRUTS,JSF…etcc)in dispatcher servlet file? i mean without ContextLoaderListener.

Thanks,
SR
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

selva raja wrote:Is it possible to configure third part framework (I.example : DWR,STRUTS,JSF…etcc)in dispatcher servlet file? i mean without ContextLoaderListener.

Thanks,
SR



If you don't have the ContextLoaderListener then you won't have your Parent Application Context with your services, repository/dao middle tier classes.

DispatcherServlet is only for Spring MVC classes in a child application context. So DispatcherServlet loads classes for the web layer. If you use Struts, then you don't need the DispatcherServlet, but will need the ContextLoaderListener if you want your Struts action classes to have access to you business logic and lower layers. If you are using Struts, the integration is to have your Action classes extend ActionSupport.

Mark
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey all.

Thing is that i made my sample project (java web project) and added Spring capabilities and hibernate capabilities. In my web.xml i defined ContextLoaderListener and gave ContextConfigLocation
************************************************** *********
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListe ner
</listener-class>

</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-hibernate.xml


</param-value>

</context-param>

************************************************** *******
applicationContext.xml is containing beans (conrollers, managers and DAO declarations)
applicationContext-hibernate.xml is containing dataSource, sessionFactory and transactionManager information.


after that i decided to add MVC module of spring and add following into web.xml

************************************************** **
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

************************************************** *****
I have added action-servlet.xml in WEB-INF folder. and provided "viewResolver" and "urlMapping". BUt when i try to run the project following error occur

**************************************************
javax.servlet.ServletException: No adapter for handler [controller.CityController@17ebdf8]: Does your handler implement a supported interface like Controller?


************************************************** *
Although I have provided CityController in action-servlet.xml

************************************************** *
This is being a headache for me since one day. Is this not the way to use Spring + Spring MVC + Hibernate

**********************************
I m using forums for the very first time. Sorry if i forget any discipline.
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please look into below spring forums get it resolved,

http://forum.springsource.org/showthread.php?t=52432

Thanks,
SR
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks for your quick reply SR...
But on the link you provided its using annotation base classes. M using just simple controller. I m send you its code...

Now is it possible to use applicationContext.xml , applicationContext-hibernate.xml and action-servlet.xml all together.......
and using DispatcherServlet and ContextLoaderListener both in xml...?
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Muddassir,

please use this subclass your controller (CityController ) from MultiActionController.

Then in your bean configuration,

<bean id="cityController MultiController" class="com.xxxxx.xxxxx.CityController ">
<property name="methodNameResolver" ref="paramResolver"/>
... other property settings...
</bean>

you need to set other configurations...etc..

Thanks,
SR
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raja... your tip is quite help full.
There is a question that I got three xml files.

1. what I define as ContextConfigLocation (two in number)

a: applicationContext.xml (In this I define beans about Controller, Manager (Business Logic) and DAO)
b: applicationContext-hibernate.xml (In this I define dataSource, sessionactory, TransactionManager)
2. What I define as DispatcherServlet
<servletName>-servlet.xml (In this I give controllers and their URLMappings.)

My question is this can i skip defining controllers in DispatcherServlet's xml as it can also pick them from applicationContext.xml. BUt when i delete controllers from <servletName>-servlet.xml file , it start generating exceptions that controller not find and all that.

What to do to make these three xmls work together?
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

DispatcherServlet will always load its own configuration file using <servlet_name>-servlet.xml. This file will contain web components such as Controllers, ViewResolvers and LocaleResolvers.( <servletName>-servlet.xml)

The ContextLoaderListener is then used to load the files containing your middle tier and data tier components. (applicationContext.xml, applicationContext-hibernate.xml).

No need to define two xml files in ContextConfigLocation. You can merge these files in single.

Thanks,
SR
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raja. I am just deleting controller from applicationContext.xml, and defining them in <servletName>-servlet.xml

and every MultiActionControler has a decalartion + bean attach to it for methodNameResolver.
Every MultiActionController has its separate methodNameResolver bean.

There is one more question, how can we set scope of beans defined in <servletName>-servlet.xml.

Regards
 
selva raja
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes

Thanks,
SR
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all.. this is my code...

1. web.xml



2. action-servlet.xml



3. CityController



Exception ocurs if I run the http://localhost:8080/guitarLesson/city.jsp

it is caling recursively. I am unable to understand what is going on. I need help.

Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic