The operating mechanism of dispatcherservlet in spring

Source: Internet
Author: User
Tags abstract continue manual file upload inheritance reference
The operating mechanism of dispatcherservlet in spring

Dispatcherservlet is the core servlet in Spring's web framework (hereinafter referred to as Springweb). " The spring web framework-like other web frameworks-is a request-driven web framework designed around a servlet that distributes requests to the controller, and it also provides additional functionality to help Web application development. "----The Spring Framework Development Reference Manual (Chinese edition)" and this servlet is org.springframework.web.servlet.DispatcherServlet in the Springweb framework. The inheritance relationship of this servlet is shown in the following illustration: Springweb first wraps the traditional HttpServlet abstract class into Bean;frameworkservlet abstraction of some basic behavior of servlets in the web framework, For example, access to the application context; Dispatcherservlet's main job is to distribute a request to a suitable processor and draw the processing returned Modelandview back to the client.

Dispatcherservlet as a servlet then he must have two main methods: Init () and Doservice ().

One init () initialization. The implementation of the Init () method in the Dispatcherservlet inheritance system is located in Httpservletbean, Httpservletbean first calls Initbeanwrapper (), initializes the Beanwrapper, Then the abstract method Initservletbean () is invoked, and the implementation of the method is in his subclass Frameworkservlet; Initservletbean in Framewordservlet () Method will call Initwebapplicationcontext (), initialize the Webapplicationcontext, and then call his abstract method Initframeworkservlet (), The implementation of this abstract method is in the final dispatcherservlet; the Initframeworkservet () in Dispatcherservlet () initializes multipar (used as a file upload) parser, localized information parser, Subject Parser processor mappings, and so on. So the initialization order of Dispatcherservlet is init (); Initbeanwrapper ();

Initservletbeaninitwebapplicationcontext ()

Initframework (); Initmultipartresolver (); Initlocaleresolver (); Initthemeresolver (); inithandlermappings (); Inithandleradapters (); Inithandlerexceptionresolvers (); Initviewresolvers ();

Two doservice () processing request. In Dispatcherservlet, the request submitted by post or get mode will eventually be handled by Doservice (). The processing logic in Doservice () is roughly divided into the following six steps: 1.if (Request is multipart, file upload) The request is parsed and packaged into Multiparthttpservletrequest2.mappedhandler = GetHandler (Request) According to request get the corresponding processor 3. Call the Prehandle Method 4 of all registered interceptors. Call Processor Handleradapter ha = new Gethandleradapter (mappedhandler.gethandler    ()); Modelandview mv = ha.handle (req, res, Mappedhandler.gethandler ())//This uses the adapter mode 5. Posthandle Method 6 for calling all registered interceptors. Draw MV

Spring may not be the most widely used, like struts and hibernate, but he is comprehensive, lightweight, flexible enough, easy to replace, and easy to expand. Springweb is a part of springframework, and Dispatcherservlet is a small portion of Springweb, to understand spring and the design behind him, for me and other rookie, still have a long way to go. I wish we had traveled this road before the new road appeared. Road long its repair far XI, I will be up and down and searching. Continue to study be continue ...

Reference Spring official website API Reference documentation Framework Development Reference Manual (Chinese version) http:/ /

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.