I. Working Principle of Servlet
Servlet runs in the servlet container. The servlet container is responsible for searching, creating, and managing servlet instances throughout the lifecycle. The entire servlet lifecycle can be divided into four stages: class loading and instance creation, instance initialization, service, and instance Destruction.
Class loading and instance creation phase
By default, the servlet instance is created when the first request is received and the subsequent requests are reused. If a servlet instance needs to perform some complex operations, you must complete the initialization, such as opening a file or initializing a network connection. You can configure to create an instance when the server is started, the specific configuration method is to add the <load-on-startup> 1 </load-on-startup> label to the declared servlet tag.
Initialize Init (servletconfig config)
Once a servlet instance is created, the inint method of the servlet will be called, and the servletconfig instance will be passed in to the servlet-related configuration information. The init method will be called only once throughout the servlet lifecycle.
Services ()
To improve efficiency, the servlet specification requires that a servlet instance must be able to serve multiple client requests at the same time, that is, the Service () method runs in a multi-threaded environment, servlet developers must ensure the thread security of this method.
Destroy destory ()
When the servlet container decides to end a servlet, it will call the destory () method to release resources in the destory method. Once the destory method is called, the servlet container will no longer send any request to this instance. If the servlet container needs to use the servlet again, it needs to instantiate the servlet instance again.
Ii. servlet Execution Process
After the Web server receives an HTTP request, the web server will hand over the request to the servlet container. The servlet container first parses the requested URL and. find the corresponding processing servlet in the XML configuration file and pass the request and response objects to it. Through the request object, the servlet can know the client requestor, request information, and other information, after processing the request, the servlet puts all the information to be returned into the response object and returns it to the client. Once the servlet finishes processing the request, the servlet container will refresh the response object, and return the control to the web server.
Iii. Comparison with other technologies
Compared with other services, Servlet has the following advantages:
1. The running speed is faster than CGI because multithreading is used.
2. servlet uses standard APIs and can be supported by many web services.
3. unrelated to the system. It is used multiple times during one compilation.
Iv. servlet container,
Servlet is a general extension of servers that support Java. Its most common purpose is to expand web servers and provide very secure, portable, and easy-to-use CGI alternatives. It is a dynamic loading module that provides services for requests from Web servers. It runs completely on the Java Virtual Machine. Because it runs on the server side, it does not depend on the browser compatibility.
Servlet container:
Handles customer requests, sends requests to servlet, and returns results to the customer. The actual implementation of containers of different programs may change, but the interfaces between containers and servlets are defined by Servlet APIs, this interface defines the servlet container's method to be called on the servlet and the object class passed to the servlet.
Servlet lifecycle:
1. Create an instance of servlet in the servlet container
2. The Container calls the init () method of the instance.
3. If the container has a request for this servlet, call the service () method of this instance.
4. The Container calls its destroy () method before destroying the instance.
5. Destroy and mark the instance for garbage collection
Once a servlet is requested, there is no way to prevent the container from executing a complete lifecycle.
The container creates an instance of the servlet when it is called for the first time, and keeps
The instance is in the memory and allows it to process all requests. The container can decide to remove the instance from the memory at any time. In a typical model, the container creates a separate entity for each servlet.
For example, the container does not create a new thread every time it receives a request, but uses a thread pool to dynamically allocate the thread to the incoming request. However, from the Servlet's point of view, results and for each
The effect of creating a new thread is the same.
5. servlet Initialization
① When the servlet is initialized, it uses Init (servletconfig config) or Init ()
.
Servletconfig is an interface. How can it be passed to another object for initialization? In fact, this object is instantiated by the servlet container. The Container generates an object of the implementation class of servletconfig and then passes it to the servlet.
② Sometimes we may give some fixed configuration parameters to the servlet during initialization. How are these parameters transmitted to the servlet? In fact, we configure the startup parameters for servlet in Web. xml.
During initialization, the parameters you configured are collected and recorded in the Implementation class of servletconfig.
③ Let's look at the configuration of a servlet.
<servlet> <servlet-name>index</servlet-name> <servlet-class>org.zy.pro.sw.servlet.IndexServlet</se rvlet-class> <init-param> <param-name>dbconfig</param-name> <param-value>/WEB-INF/dbconfig.xml</param-value> </init-param></servlet>
Servlet initialization in Web. XML can only pass string-type data
④ Servletcontext
Servletcontext is responsible for interacting with the servlet above and below, and interacting with the servlet container below, and the servlet container below
Request in
And the corresponding interaction. In servletconfig, public servletcontext
Getservletcontext (); method to obtain the object of the current servletcontext. You may want to ask,
Servletcontext is an interface. How do you obtain its objects?
In fact, this problem is the same as that of servletconfig.
Objects generated during initialization are initialized by containers.
Refer:
Http://persevere.iteye.com/blog/952866
Http://developer.51cto.com/art/200907/135095.htm