Servlet
There are the following four stages:
1. Loading and instantiation
The servlet container is responsible for loading and instantiating the servlet.
The servlet instance is created when the servlet container is started, or when the container detects that the servlet is needed to respond to the first request.
When the servlet container is started, it must know where the servlet class is needed, and the servlet container can load the servlet class from the local file system, the remote file system, or other network services through the class loader.
After a successful load, the container creates an instance of the servlet.
Because the container is creating the servlet instance through the Java Reflection API, calling the default constructor method of the servlet (that is, the constructor without parameters), so when we write the servlet class,
You should not provide a construction method with parameters.
2. Initialization
After the servlet is instantiated, the container will invoke the servlet's init () method to initialize the object.
The purpose of initialization is to have the Servlet object do some initialization work before processing the client request, such as establishing a connection to the database, obtaining configuration information, and so on.
For each servlet instance, the init () method is called only once. During initialization, the servlet instance can use the ServletConfig object that the container prepares for it from the Web application's configuration information (configured in XML. config)
Gets the initialized parameter information in the
During initialization, if an error occurs, the servlet instance can throw a Servletexception exception or Unavailableexception exception to notify the container.
The servletexception exception is used to indicate a general initialization failure, such as no initialization parameters found;
The unavailableexception exception is used to notify the container that the servlet instance is not available.
For example, if the database server is not started and the database connection cannot be established, the servlet can throw a unavailableexception exception to the container to indicate that it is temporarily or permanently unavailable.
3. Request Processing
The servlet container invokes the servlet's service () method to process the request.
It is important to note that the init () method must execute successfully before the service () method call.
In the service () method, the Servlet instance obtains information about the client and request information through the ServletRequest object, and, after processing the request, invokes the method of the Servletresponse object to set the response information.
During the service () method execution, if an error occurs, the servlet instance can throw a Servletexception exception or unavailableexception exception.
If the unavailableexception exception indicates that the instance is permanently unavailable, the servlet container invokes the instance's destroy () method to release the instance.
Any subsequent requests for that instance will receive the HTTP 404 (requested resource unavailable) response sent by the container. If the unavailableexception exception indicates that the instance is temporarily unavailable,
In the temporarily unavailable time period, any request for that instance will receive the HTTP 503 (the server is temporarily busy, unable to process the request) response sent by the container.
4. Termination of Service
When the container detects that a servlet instance should be removed from the service, the container invokes the instance's destroy () method so that the instance can free the resources it uses and save the data to the persistent storage device.
When the memory needs to be freed or the container is closed, the container invokes the Destroy () method of the servlet instance. After the Destroy () method call, the container releases the servlet instance,
The instance is then reclaimed by the Java garbage collector.
If the servlet is required to process the request again, the servlet container creates a new servlet instance.
can also be summarized as:
The servlet program is called by the Web server after the Web server receives a servlet access request from the client: the ①web server first checks whether the instance object of the servlet has been loaded and created. If it is, execute step ④ directly, otherwise, step ②. ② mounts and creates an instance object of the servlet. ③ invokes the Init () method of the Servlet instance object. ④ creates a HttpServletRequest object that encapsulates an HTTP request message and a HttpServletResponse object that represents an HTTP response message.
* Note
During the lifetime of the servlet, the creation of the servlet instance, the init (), and Destroy () methods of the calling instance are performed only once,
When the initialization is complete, the servlet container saves the instance in memory by invoking its service () method, which serves the received request.
Struts2
1. Flowchart:
2. Process Description:
1.The client sends the request;2.this request passes through a series of filters (filter) with an optional filter called Actioncontextcleanup, which is useful for integration of Struts2 and other frameworks,
Example: Sitemesh Plugin)3.then Filterdispatcher is called, Filterdispatcher asks Actionmapper to decide if it needs to invoke an action. The functions of Filterdispatcher are as follows: (1) perform the actions (2) Clear the Actioncontext (3) maintain static content (4) Clears the interceptors of the xwork in the request life cycle4.if Actionmapper decides to call one of the action,filterdispatcher to give Actionproxy the request processing5.Actionproxy to find the action class that needs to be called by using Configuration Manager to ask for the framework profile6.Actionproxy Create an instance of Actioninvocation. 7.The actioninvocation instance is invoked using a naming pattern that involves a call to the relevant interceptor (Intercepter) before and after the procedure that invokes the action. 8. Once the action is executed, Actioninvocation is responsible for finding the corresponding return result based on the configuration in the Struts.xml.
The return result is usually (but not always, possibly another action chain) a JSP or freemarker template that needs to be represented.
The tags inherited in the STRUTS2 framework can be used in the representation process. Need to involve actionmapper in this process
Details Link: struts2 Execution principle (execution flow)
Springmvc
1. Flowchart:
2. Process Description:
1the user sends a request to the server and the request is captured by the spring front-end control Servelt Dispatcherservlet;2. Dispatcherservlet parses the request URL to get the request Resource Identifier (URI). Then, based on the URI, call handlermapping to get all the related objects for that handler configuration
(includes the Handler object and the interceptor for the handler object), which is then returned as a Handlerexecutionchain object. 3. Dispatcherservlet according to the obtained handler, choose a suitable handleradapter. (Note: If Handleradapter is successfully obtained, the Interceptor Prehandler (...) will start executing. method)4. Extract the model data from the request, populate the handler entry, and start executing the handler (Controller).
In the process of populating the handler, depending on your configuration, spring will do some extra work for you: 1) Httpmessageconveter: Converts a request message (such as JSON, XML, and so on) into an object, converts the object to the specified response information 2) data to Change: Data conversion for the request message. such as string conversion to Integer, double, etc. 3) data is being initialized: Data is formatted for the request message. such as converting a string to a formatted number or a formatted date, etc. 4) data validation: Verifying the validity of the data (length, format, etc.), the validation results are stored in Bindingresult or error5. Handler returns a Modelandview object to Dispatcherservlet when execution is complete;6According to the returned Modelandview, select a suitable viewresolver (must be viewresolver already registered in the spring container) to return to Dispatcherservlet;7. Viewresolver combining model and view to render the view8. Return the rendering results to the client.
It can also be said that:
The primary content of process normalization is to consider the logical steps that a generic servlet responder should roughly include: step-by-take the HTTP request to the initial processing, find the corresponding Controller processing class (method) -- Handlermapping Step 2--Call the appropriate Controller processing class (method) to complete the business logic --handleradapter Steps 3--handling exceptions that may occur when a controller processes a class (method) call The--handlerexceptionresolver step 4--HTTP Response processing based on the call result of the Controller processing class (method) --viewresolver
comparison of Struts2 and Springmvc
Article link: Comparison of Springmvc and Struts2
Servlet, Struts2, SPRINGMVC execution process