Sample Chapter 13 - Manning Publications
Sample Chapter 13 - Manning Publications
Sample Chapter 13 - Manning Publications
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>13</strong>.1.1 A day in the life of a request<br />
Getting started with Spring MVC 491<br />
Every time that a user clicks a link or submits a form in their web browser, a<br />
request goes to work. A request’s job description is that of a courier. Just like a<br />
postal carrier or a Federal Express delivery person, a request lives to carry information<br />
from one place to another.<br />
The request is a busy fellow. From the time that it leaves the browser until the<br />
time that it returns a response, it will make several stops, each time dropping off a<br />
bit of information and picking up some more. Figure <strong>13</strong>.1 shows all the stops that<br />
the request makes.<br />
When the request leaves the browser, it carries information about what the<br />
user is asking for. At very least, the request will be carrying the requested URL. But<br />
it may also carry additional data such as the information submitted in a form by<br />
the user.<br />
The first stop in the request’s travels is Spring’s DispatcherServlet B. Like<br />
most Java-based MVC frameworks, Spring MVC funnels requests through a single<br />
front controller servlet. A front controller is a common web-application pattern<br />
where a single servlet delegates responsibility for a request to other components<br />
of an application to perform the actual processing. In the case of Spring MVC,<br />
DispatcherServlet is the front controller.<br />
The DispatcherServlet’s job is to send the request on to a Spring MVC controller.<br />
A controller is a Spring component that processes the request. But a typical<br />
application may have several controllers and DispatcherServlet needs help<br />
deciding which controller to send the request to. So, the DispatcherServlet<br />
B<br />
Request Dispatcher<br />
Servlet<br />
C<br />
G<br />
Handler<br />
Mapping<br />
ModelAndView<br />
F<br />
View<br />
D<br />
E<br />
Controller<br />
ViewResolver<br />
Figure <strong>13</strong>.1 A request is dispatched by DispatcherServlet to a<br />
controller (which is chosen through a handler mapping). Once the<br />
controller is finished, the request is then sent to a view (which is<br />
chosen through a ViewResolver) to render output.