5

In which scenarios, we need multiple Dispatcher-Servlets?
Can anyone please tell me the use cases of multiple Dispatcher-Servlets.
I think every use case can be solved by using single Dispatcher Servlet.

PraveenKumar Lalasangi
  • 3,255
  • 1
  • 23
  • 47
Mahesh Bhuva
  • 744
  • 5
  • 17
  • 1
    With multiple dispatcher servlets you can have multiple application contexts (a different one for each): https://stackoverflow.com/q/12059307/14955 – Thilo Nov 29 '18 at 07:00
  • 1
    I read that already but I wasn't able to understand the need for multiple Dispatcher Servlets. – Mahesh Bhuva Nov 29 '18 at 07:04
  • 2
    The need is for multiple application contexts (for different parts of the application). To enable that, you can use multiple dispatcher servlets. – Thilo Nov 29 '18 at 07:09

3 Answers3

4

From Documentation

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.

Advantages of multiple dispatcher servlets OR Why we need multiple dispatcher servlets? OR When do we need multiple dispatcher servlets?

Simple answer is to have DispatcherServlet's functionality in several forms

Dispatcher servlet functionality



I will try to explain some of the functionalities provided by DispatcherServlet

Declaring Multiple dispatcher servlets
Consider we have two dispatcher servlets(DS) where DS1, DS2 are configured with different url patterns(**.simple, **.beanName) and they uses different dispatcher servlet configuration provided as given below.

DispatcherServlet     - simpleUrlHandlerDispatcherServlet
contextConfigLocation - /WEB-INF/simpleUrlHandlerMapping.xml
<url-pattern>*.simple</url-pattern>

DispatcherServlet     - beanNameUrlHandlerDispatcherServlet
contextConfigLocation - /WEB-INF/beanNameUrlHandlerMapping.xml
<url-pattern>*.beanName</url-pattern>

Advantage 1: We can have different HandlerMapping for different set of URL

DS1 bean name url handler mapping configuration

<bean name="/hello.beanName" class="com.pvn.mvc.HelloController" />
<bean name="/hi.beanName" class="com.pvn.mvc.HiController" />

DS2 simple url handler mapping configuration

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/hello.simple">simpleHello</prop>
            <prop key="/hi.simple">simpleHi</prop>
        </props>
    </property>
</bean>

Advantage 2: We can have different view resolver for different set of URL's.

InternalResourceViewResolver for DS1
where it deals with only prefix + returned String + suffix.
TilesViewResolver for DS2
its implementation provided by apache tiles which is a layout/skeleton based plugin high level functionality as given below.
enter image description here Or if we use TilesViewResolver with different layout for different set of URL's
anonymous user - different layout
logged in user - different layout

Advantage 3: We can have different theme resolver for different set of URL's.
These resolver continuously monitors cookie/session to resolve theme and serves stylesheet/theme qualified (as given in below image). Below given just an example for outcome of CookieThemeResolver.
Note: This is not about theme configuration but about theme resolver configuration.

enter image description here

Advantage 4: We can have different locale resolver for different set of URL's.
These resolver continuously monitors cookie/session/accept-header to resolve locale and loads application message qualified(as given in below image). Below given just an example for outcome of CookieLocaleResolver.
Note: This is not about locale configuration but about locale resolver configuration.
enter image description here

PraveenKumar Lalasangi
  • 3,255
  • 1
  • 23
  • 47
  • My concern here is since you have several DispatcherServlets, each for a different purpose, won't it be an unwanted overhead for managing them in the long run. I agree with [M. Deinum in his answer](https://stackoverflow.com/a/23053029/2788547) to this same question. – Aniket Jun 25 '21 at 17:52
2

As DispatcherServlet is very flexible. Not only Spring MVC uses it, but also Spring WS and etc.

In general, we declare multiple dispatcher servlets when we need multiple sets of MVC configuration. For example, we may have a REST API alongside a traditional MVC application or an unsecured and a secure section of a website:

Diagram

Source of this answer, read in detail...

0

Strictly seperating user and admin functionality. Or one for plain Spring MVC and another for Spring Web Flow. If there are major configuration differences for some controllers. (We actually used the Spring MVC and Spring Web Flow seperation so that we could add this without affecting the already exising configs). Nowadays with servlet 3.0 you could develop seperate parts of your application seperatly and have all of them contribute there own DispatcherServlet and mappings (although you could also achieve this with some configuration conventions).

Alien
  • 15,141
  • 6
  • 37
  • 57