202001220 - servlet rookie learning

What is Servlet
Java Servlet is a program running on a server or application server, which is used as an intermediate layer between the database or applications on request and Http server from a Web browser or other Http client.

Servlet life cycle
Servlet life cycle can be defined as the entire process from creation to destruction. The following is the procedure to follow Servlet:
Servlet is initialized by calling the init () 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.

The init method
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.

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.

public void init() throws ServletException {
  // 初始化代码...
}

service method
service () method is the primary 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.

public void service(ServletRequest request, 
                    ServletResponse response) 
      throws ServletException, IOException{
}

doGet () method
GET request from a normal URL request, or from an unspecified METHOD HTML form, it is processed by doGet () method.

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

doPost () method

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:

  public void destroy() {
    // 终止化代码...
  }

Chart
The following figure shows a typical Servlet life cycle program.

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.


Servlet form data
in many cases, need to pass some information from the browser to the Web server, and ultimately to the daemon. Browser can use two methods to pass this information to the Web server are GET and POST methods.

GET method
GET method is the default method of conveying information from the browser to the Web server, it will produce a long string, it appears in the address bar of your browser. If you want to pass a server passwords or other sensitive information, please do not use the GET method. GET method is limited in size: the request string can only have up to 1024 characters.

QUERY_STRING using header information transfer, and can be accessed through the QUERY_STRING environment variable, Servlet using doGet () method to handle this type of request.

POST method
more reliable method of transmitting information to another daemon is POST method. How POST way to package information with the GET method is basically the same, but the method is not POST the information as a URL? Text string of characters sent, but this information as a separate message. When the news reached daemon as a standard output, you can parse and use them to standard output. Servlet using doPost () method to handle this type of request.

Servlet using form data read
Servlet form data processing, the data may use different methods depending on the automatic analysis:

getParameter (): You can call request.getParameter () method to get the value of the form parameter.
getParameterValues (): If the parameter appears more than once, this method is called, and returns multiple values, e.g. checkbox.
getParameterNames (): If you want to get a complete list of all the parameters of the current request, then the method call.

Http Servlet client requests
Here Insert Picture Description

Servlet status code
Here Insert Picture Description
Here Insert Picture Description

Published 666 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/104057389