Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple application context, multiple dispatcher servlets?

Tags:

Until now I used to think that a web-application can have only one dispatcher-servlet which we define in web.xml

  • Am I right in thinking so?
  • Can i have multiple dispatcher servlets in a single web application? If yes, How?
  • What is a situation we might need this in?
  • Can there only be a single application context in the entire web application?
  • How can we define multiple application contexts?
  • Can a dispatcher-servlet exist in a non-spring application?
like image 705
Phoenix Avatar asked Aug 21 '12 16:08

Phoenix


People also ask

Can we have multiple dispatcher servlets?

You can have as many DispatcherServlets as you want. Basically what you need to do is duplicate the configuration and give the servlet a different name (else it will overwrite the previous one), and have some separate configuration classes (or xml files) for it.

Can we have more than one dispatcher servlet in Web xml?

You can configure multiple DispatcherServlet instances, each having its own configuration like en-servlet. xml, ib-servlet. xml etc.

Why do we need multiple dispatcher servlet in Spring?

A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.

Can Spring have multiple application contexts?

We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.


1 Answers

Can you have multiple dispatcher servlets in a single web application ?

Of course, quoting the official documentation (bold is actually there as well!)

A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.


How?

Just declare several servlets with different names but using org.springframework.web.servlet.DispatcherServlet class. Also make sure yourServletName-servlet.xml file is available.


What is a situation we might need this in ?

DispatcherServlet is very flexible. Not only Spring MVC uses it, but also Spring WS, Spring support for hessian, etc.


Also, can there only be a single application context in the entire web application ?

Answered already, also in the quoted documentation: one application context per DispatcherServlet + one main web application context.


How can we define multiple application contexts ?

See above, just create multiple DispatcherServlets.


Can a dispatcher servlet exist in a non-spring application ?

DispatcherServlet is a Spring context (Spring application) on its own, thus: no. On the hand DispatcherServlet can be declared in an application not having parent (main) application context, thus: yes.

like image 128
Tomasz Nurkiewicz Avatar answered Dec 10 '22 22:12

Tomasz Nurkiewicz