Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
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 ();
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 ing...to be continue ...
Reference Spring official website http://www.springframework.org/Spring API http://www.springframework.org/docs/api/index.htmlSpring Reference documentation http://www.springframework.org/docs/reference/index.htmlSpring Framework Development Reference Manual (Chinese version) http:/ /www.jactiongroup.net/reference/html/index.html
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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.