Java web Learning (II) Servlet life cycle

  • Servlet by calling the  init ()  to initialize method.
  • Servlet call  service ()  method to process client requests.
  • Servlet by calling  destroy ()  method is terminated (end).
  • Finally, Servlet is garbage collected by the JVM's garbage collector.

Be careful to explore next.

init () method

The init method is designed to call only once. It is called when you first create a Servlet, it is no longer called upon each subsequent user request. Therefore, it is used for one-time initialization, like the init method of Applet.

When the Servlet to create a user corresponding to the first call to the Servlet URL, but you can also specify the Servlet is loaded when the server is first started.

void the init public () throws ServletException { 
  // Initialization code ... 
}

When the user invokes a Servlet, it will create a Servlet instance, each user request results in a new thread, transfer the appropriate time to doGet or doPost methods. init () method simply create or load some data that will be used throughout the life cycle Servlet.

service () method

service () method is the main method performs the actual task. Servlet container (ie, Web server) calls the service () method to handle requests from the client (browser), and to write the formatted response back to the client.

Each time the server receives a request Servlet, the server will generate a new thread and call service. service () method checks the type of HTTP request (GET, POST, PUT, DELETE, etc.), and calls doGet, doPost, doPut, doDelete the like at the appropriate time.

@Override
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
        }
 }

ServletRequest: encapsulates the request information may be obtained from any request information.

ServletResponse: encapsulated response information for responding to a user request.

service () method is called by a container, service method calls doGet, doPost, doPut, doDelete the like at the appropriate time. Therefore, we do not need to service () operation, only you need to rewrite doGet () or doPost () according to the type of the request from the client.

doGet () method

Apparent, doGet () to process the request is GET or METHOD not specified or HTML form.

doPost () method

POST request from the HTML form specifies a particular METHOD of POST, which is processed by doPost () method.

doGet () and doPost () are used in the basic and the corresponding http request which

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet 代码
}
public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
    throws ServletException, IOException {
    // Servlet 代码
}

destroy () method

destroy () method is invoked only once, to be called at the end of the Servlet life cycle. destroy () method allows you to turn off the Servlet database connection, stop the background thread, list, or click on the Cookie counter is written to disk, and perform other similar clean-up activities.

After calling destroy () method, servlet object is marked as garbage collection. destroy method defined as follows:

void the destroy public () { 
    // ... termination code 
  }

 

 

  • The first HTTP request arrives at the server is delegated to the Servlet container.
  • Servlet Servlet container loaded before calling the service () method.
  • Then Servlet container handle multiple requests generated by a plurality of threads, each thread of execution in a single Servlet examples of service () method.
  •  

Guess you like

Origin www.cnblogs.com/zhangrj9/p/11364077.html