<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Spring on 0AndWild_log</title><link>https://0andwild.com/en/series/spring/</link><description>Recent content in Spring on 0AndWild_log</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Tue, 27 Sep 2022 23:10:15 +0900</lastBuildDate><atom:link href="https://0andwild.com/en/series/spring/index.xml" rel="self" type="application/rss+xml"/><item><title>Understanding Spring Dispatcher Servlet</title><link>https://0andwild.com/en/posts/220927_dispatcher/</link><pubDate>Tue, 27 Sep 2022 23:10:15 +0900</pubDate><guid>https://0andwild.com/en/posts/220927_dispatcher/</guid><description>&lt;img src="https://0andwild.com/" alt="Featured image of post Understanding Spring Dispatcher Servlet" /&gt;&lt;h2 id="what-is-dispatcher-servlet"&gt;&lt;a href="#what-is-dispatcher-servlet" class="header-anchor"&gt;&lt;/a&gt;What is Dispatcher Servlet?
&lt;/h2&gt;&lt;p&gt;The term &lt;strong&gt;dispatch&lt;/strong&gt; in Dispatcher Servlet means &amp;ldquo;to send&amp;rdquo;. The Dispatcher Servlet can be defined as a &lt;strong&gt;Front Controller that receives all incoming HTTP protocol requests first and delegates them to the appropriate controller&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id="operation-overview"&gt;&lt;a href="#operation-overview" class="header-anchor"&gt;&lt;/a&gt;Operation Overview
&lt;/h3&gt;&lt;p&gt;The more detailed process is as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;When a request comes from the client, a servlet container such as &lt;strong&gt;Tomcat&lt;/strong&gt; receives the request&lt;/li&gt;
&lt;li&gt;All these requests are received first by the &lt;strong&gt;Dispatcher Servlet&lt;/strong&gt;, which is the Front Controller&lt;/li&gt;
&lt;li&gt;The Dispatcher Servlet processes common tasks first and then finds the controller that should handle the request and delegates the work&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="front-controller-pattern"&gt;&lt;a href="#front-controller-pattern" class="header-anchor"&gt;&lt;/a&gt;Front Controller Pattern
&lt;/h3&gt;&lt;p&gt;The term &lt;strong&gt;Front Controller&lt;/strong&gt; refers to a controller that receives and processes all client requests coming to the server at the front of the servlet container, and it is a &lt;strong&gt;design pattern used together with the MVC architecture&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="how-dispatcher-servlet-works"&gt;&lt;a href="#how-dispatcher-servlet-works" class="header-anchor"&gt;&lt;/a&gt;How Dispatcher Servlet Works
&lt;/h2&gt;&lt;p&gt;The Dispatcher Servlet is the &lt;strong&gt;Front-Controller&lt;/strong&gt; that receives requests first.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It passes through filters in the &lt;strong&gt;Servlet Context (Web Context)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The Dispatcher Servlet receives the request first in the &lt;strong&gt;Spring Context&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Figure 1: Servlet Context and Spring Context" class="gallery-image" data-flex-basis="601px" data-flex-grow="250" height="505" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://0andwild.com/posts/220927_dispatcher/figure1.png" srcset="https://0andwild.com/posts/220927_dispatcher/figure1_hu_2b8ebc4042004c11.png 800w, https://0andwild.com/posts/220927_dispatcher/figure1.png 1265w" width="1265"&gt;&lt;/p&gt;
&lt;p&gt;The Dispatcher Servlet must find the appropriate controller and method to delegate the request, and the operation process is as follows.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Figure 2: Dispatcher Servlet Flow" class="gallery-image" data-flex-basis="511px" data-flex-grow="213" height="560" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://0andwild.com/posts/220927_dispatcher/featured.png" srcset="https://0andwild.com/posts/220927_dispatcher/featured_hu_e8f8df7da441261c.png 800w, https://0andwild.com/posts/220927_dispatcher/featured.png 1193w" width="1193"&gt;&lt;/p&gt;
&lt;h3 id="detailed-operation-process"&gt;&lt;a href="#detailed-operation-process" class="header-anchor"&gt;&lt;/a&gt;Detailed Operation Process
&lt;/h3&gt;&lt;h4 id="1-http-request-passes-through-filter-and-is-received-by-dispatcher-servlet"&gt;&lt;a href="#1-http-request-passes-through-filter-and-is-received-by-dispatcher-servlet" class="header-anchor"&gt;&lt;/a&gt;1. HTTP Request passes through Filter and is received by Dispatcher Servlet
&lt;/h4&gt;&lt;h4 id="2-check-request-information-and-find-the-controller-to-delegate"&gt;&lt;a href="#2-check-request-information-and-find-the-controller-to-delegate" class="header-anchor"&gt;&lt;/a&gt;2. Check request information and find the Controller to delegate
&lt;/h4&gt;&lt;p&gt;&lt;code&gt;RequestMappingHandlerMapping&lt;/code&gt;, one of the implementations of &lt;code&gt;HandlerMapping&lt;/code&gt;, parses all controller beans written with &lt;code&gt;@Controller&lt;/code&gt; and &lt;strong&gt;manages (request information, processing target) as a HashMap&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;It finds the &lt;code&gt;HandlerMethod&lt;/code&gt; object that contains the controller and method mapped to the request. Therefore, when a request comes in, &lt;code&gt;HandlerMapping&lt;/code&gt; creates a Key object (request information) using HTTP Method, URI, etc., finds the &lt;code&gt;HandlerMethod&lt;/code&gt; to process the request as Value, wraps it in &lt;code&gt;HandlerMethodExecutionChain&lt;/code&gt;, and returns it.&lt;/p&gt;
&lt;p&gt;The reason for this wrapping is to &lt;strong&gt;include interceptors that need to be processed before passing the request to the controller&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id="3-find-and-pass-the-handleradapter-to-delegate-to-the-controller"&gt;&lt;a href="#3-find-and-pass-the-handleradapter-to-delegate-to-the-controller" class="header-anchor"&gt;&lt;/a&gt;3. Find and pass the HandlerAdapter to delegate to the Controller
&lt;/h4&gt;&lt;p&gt;The Dispatcher Servlet does not delegate requests directly to the controller, but delegates them through &lt;code&gt;HandlerAdapter&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The reason for going through the &lt;code&gt;HandlerAdapter&lt;/code&gt; interface is that &lt;strong&gt;there are various ways to implement controllers&lt;/strong&gt;. While controller classes are mainly written using &lt;code&gt;@Controller&lt;/code&gt; with &lt;code&gt;@RequestMapping&lt;/code&gt; related annotations, controller classes can also be written by implementing the &lt;code&gt;Controller&lt;/code&gt; interface.&lt;/p&gt;
&lt;p&gt;Therefore, Spring applies the &lt;strong&gt;adapter pattern&lt;/strong&gt; through the &lt;code&gt;HandlerAdapter&lt;/code&gt; interface, allowing requests to be delegated to Controllers regardless of the controller implementation method.&lt;/p&gt;
&lt;h4 id="4-handleradapter-delegates-the-request-to-the-controller"&gt;&lt;a href="#4-handleradapter-delegates-the-request-to-the-controller" class="header-anchor"&gt;&lt;/a&gt;4. HandlerAdapter delegates the request to the Controller
&lt;/h4&gt;&lt;p&gt;Common pre/post processing is required before &lt;code&gt;HandlerAdapter&lt;/code&gt; passes the request to the Controller.&lt;/p&gt;
&lt;p&gt;Typically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Interceptor processing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ArgumentResolver&lt;/strong&gt; to handle &lt;code&gt;@RequestParam&lt;/code&gt;, &lt;code&gt;@RequestBody&lt;/code&gt;, etc. in requests&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ReturnValueHandler&lt;/strong&gt; that handles processing such as serializing the Body of &lt;code&gt;ResponseEntity&lt;/code&gt; to JSON in responses&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These processes are handled before being passed from the adapter to the controller. Then it delegates the request to invoke the controller&amp;rsquo;s method.&lt;/p&gt;
&lt;h4 id="5-process-business-logic"&gt;&lt;a href="#5-process-business-logic" class="header-anchor"&gt;&lt;/a&gt;5. Process Business Logic
&lt;/h4&gt;&lt;p&gt;The Controller calls the service and proceeds with business logic.&lt;/p&gt;
&lt;h4 id="6-controller-returns-the-return-value"&gt;&lt;a href="#6-controller-returns-the-return-value" class="header-anchor"&gt;&lt;/a&gt;6. Controller returns the return value
&lt;/h4&gt;&lt;p&gt;Returns &lt;code&gt;ResponseEntity&lt;/code&gt; or View name.&lt;/p&gt;
&lt;h4 id="7-handleradapter-processes-the-return-value"&gt;&lt;a href="#7-handleradapter-processes-the-return-value" class="header-anchor"&gt;&lt;/a&gt;7. HandlerAdapter processes the return value
&lt;/h4&gt;&lt;p&gt;&lt;code&gt;HandlerAdapter&lt;/code&gt; returns the response received from the controller to the Dispatcher Servlet after post-processing by the &lt;code&gt;ReturnValueHandler&lt;/code&gt;, the response processor.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the controller returns &lt;code&gt;ResponseEntity&lt;/code&gt; → &lt;code&gt;HttpEntityMethodProcessor&lt;/code&gt; uses &lt;code&gt;MessageConverter&lt;/code&gt; to serialize the response object and set the response status (&lt;code&gt;HttpStatus&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;If View name is returned → View is returned through &lt;code&gt;ViewResolver&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="8-send-the-servers-response-to-the-client"&gt;&lt;a href="#8-send-the-servers-response-to-the-client" class="header-anchor"&gt;&lt;/a&gt;8. Send the server&amp;rsquo;s response to the client
&lt;/h4&gt;&lt;p&gt;The response returned through &lt;code&gt;DispatcherServlet&lt;/code&gt; passes through the Filter again and is returned to the client.&lt;/p&gt;</description></item></channel></rss>