Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Dispatcher Servlet Spring MVC

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

I have the following questions:

1. What is the need of the Dispatcher servlet in Spring MVC when there is a request mapping annotation to map the URL to the method in the controller?

2. In continuation to the above question, why there is no need for the Dispatcher servlet in a spring boot project since it also uses request mapping?
 
Sheriff
Posts: 22575
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. The dispatcher servlet is the entry point of the Spring MVC application. It uses the request mapping annotations to find the right controller and controller method for a request. Because in the end, even though you don't see it, Spring MVC is still using servlets as backing technology. That's just all shielded from you. The same is the case for JAX-RS (e.g. Jersey, RESTEasy). These too use a dispatcher servlet that calls the right controller and controller method.

2. Spring Boot does have a dispatcher servlet. It is auto-configured using class DispatcherServletAutoConfiguration, which means that Spring Boot provides it for you. You don't have to manually create a bean for it, you only need to tweak those configuration properties you need (or none at all to use defaults).
 
Saloon Keeper
Posts: 24866
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever  URL request (including ReST and other non "web page" requests) comes in to a webapp server, the first thing the server does is pick the URL apart to see which web application deployed in the server will process the request. Spring Boot is not any different, since it has an embedded webapp server (such as Tomcat) inside it.

Once the destination webapp is known, the server checks its list of servlet mappings (originally made in /WEB-INF/web.xml and now often via annotations. If the resource path part of the URL matches a servlet mapping, then the request gets passed to that servlet. If it matches a JSP, then the request gets passed to the JSP. And JSPs are internally compiled to produce servlets. If nothing else matches, the webapp server looks for a resource within its WAR that has a pathname match, and copies that resource to the response output. If nothing matches at all, then a "404 - Resource Not Found" web page is generated and returned.

The annotations for ReST and Spring Web are just convenient shortcuts. You still have to have a dispatcher servlet, because anything that has active logic in it has to have some sort of servlet to invoke that logic - even if the servlet was compiled out of a JSP. Indeed, since many MVC web models use a dispatcher servlet for the business logic and a JSP for a display template, you can effectively have one servlet feeding another servlet.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic