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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…