Until now I used to think that a web-application can have only one dispatcher-servlet
which we define in web.xml
dispatcher-servlet
exist in a non-spring application? 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.
You can configure multiple DispatcherServlet instances, each having its own configuration like en-servlet. xml, ib-servlet. xml etc.
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.
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.
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 DispatcherServlet
s.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With