Daily--SPRINGMVC system architecture and process review of program apes

Source: Internet
Author: User
Tags tomcat tomcat server
Daily--SPRINGMVC system architecture and process Review of program apes

Web development has been going on for a long time, and it's almost more than 10 years in the country. From the very beginning of the process level to the present MVC has undergone a lot of improvements and optimizations, this article mainly review the knowledge of spring MVC.
Development History first stage CGI process response

At this stage, the server is weaker and the request is simple, that is, the user sends a request, the server receives the new process, and then returns the result.

This way the price is very large, each time the new process, very troublesome. second-stage servlet thread-level response

The servlet structure is similar to the above, except that each time a new thread is created, the cost is much smaller. the life cycle of a servlet has four phases:

1 loading and instantiating: Starting Tomcat, a servlet container that loads a servlet class based on a configuration file and instantiates it with the new method
2 initialization: Then the init () method is initialized, and each servlet is initialized only once and can be understood as a singleton pattern
3 Request Processing: When the server receives the request, the thread that receives the request finds the corresponding servlet and invokes the service () method response. So there is a case where multiple threads are dropping a servlet instance at the same time, so there is a thread security problem.
4 Destroy: When Tomcat shuts down, call Destroy () to destroy the container. so the whole process is this:

1 client sends the request, the Tomcat server receives the request, encapsulates the HttpRequest object and the HttpResponse object
2 According to the configuration file XML to find the matching servlet-name, and load the corresponding servlet
3 If it has not been loaded before, it is loaded and instantiated and initialized, and if loaded, the service method is called directly to process
4 wrapping the result of the processing in a HttpResponse return

So how do you answer whether Serlet is thread-safe or not? It can be said that itself is stateless, if not in the inside of a new operation of what count++, there will be no thread security issues. If you want to avoid thread safety issues, you can use the following ideas:

1 Avoid using instance variables
2 avoiding the use of non-thread-safe collections
3 access to external writable files requires lock

In summary, here are just a few things to note about the life cycle of the servlet and thread safety. Phase III Jsp+model1

This phase introduces the JSP technology, the Java Server Page, which is a technical language that blends HTML with java. I remember when I first learned Java, that is, using this JSP technology, if the page is a little more complex, the code will be particularly confusing.

However, this approach has also introduced a front-end separation development cooperation model, that will have a special development of static pages of the people, after the development of the page to the latter part of the program Ape, the incremental development of Java-related backend processing and data presentation related functions. The overall process is

1 The user sends a request to the server, and the server corresponding to the JSP page receives the request.
2 JSP will be compiled into a servlet, the pattern will be the same as before
3 Last fill data, return. In other words, it actually merges the part of the previous Page view with the part of the servlet.

The technology model is now largely invisible. fourth phase, front-end separation +spring MVC

Now most of the patterns are like this, just slightly different in the latter part of the presentation. The main key to this model is the controller, which is responsible for the task's distribution requests, as well as the return of the data.

The schema model is as shown above, but there are two controllers in Springmvc, one is the front controller and the other is the application controller.

The approximate process is:

1 user send request, Front Controller unified receive
2 then distribute to the corresponding application controller according to different rules, for example, according to the URL
3 Application controller in call Logic code processing
4 the last layer returns.

At present, the general company, is the use of front-end separation technology structure.
The 1 frontend is vue.js or Angularjs or jquery, which is sent to the backend via HTTP.
2 The backend receives the request, processes it according to certain business rules, and then returns the data to the front end.
3 Front end is parsed by JavaScript code and browser rendering is displayed. Source Details

Through the above description, the overall process of SPRINGMVC should have a general understanding. But the classic sentence, talk is cheap, show me your code.

How does this dispacther dispatcher come true? In fact, it's just a normal servlet, except for all requests when the servlet intercepts the request:

<servlet>
 <servlet-name>test</servlet-name>
 <servlet-class>org.springframework .web.servlet.dispatcherservlet</servlet-class>
 <load-on-startup>1</load-on-startup> </ servlet> <servlet-mapping>
 <servlet-name>test</servlet-name>
 <url-pattern>/& Lt;/url-pattern> </servlet-mapping>

The servlet then calls the Dodispatch method, and the main content is here

protected void Doservice (HttpServletRequest request, httpservletresponse response) throws Exception {
 omit code try {  Dodispatch (request, response);
 } finally {
 omit code
 }
}

The Dodispatch method contains the steps described earlier:

protected void Dodispatch (HttpServletRequest request, httpservletresponse response) throws Exception {
 omit try {
  
    omit try {//Determine handler for the current request. Get processor mappings
 Mappedhandler = gethandler (pr         Ocessedrequest); Determine handler adapter for the current request. Get adapter Object
 Handleradapter ha = Gethandleradapter (mappedhandl         Er.gethandler ()); Actually invoke the handler. Actual processing
 
   
Last returned result}} }
Common Experience

1 if the development of a restful backend program, that is, through HTTP and get, POST, PUT, delete and other data additions and deletions, then you can directly use @restcontroller annotations

2 usually engineering design will be divided into several layers, controller,service,mapper if there is a hierarchy, you can use @service and @autowired annotations with automatic injection

3 If you use @service, it is best to write the service name directly, such as @service (value = "MyService") otherwise, if your name is Abcservice, the default service name case will easily cause a bug. Especially if you need to manually find a bean.

4 generally in order to make the code concise, the controller parameter list can encapsulate a JavaBean class, used to automatically encapsulate parameters, which is much more convenient when used.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.