HTTP servlet basic Review 2

Source: Internet
Author: User

Httpservlet is used as an abstract class to create your own HTTP
The servlet. httpservlet class extends the gennericservlet class. The subclass of the httpservlet class must override at least one of the following methods:
The doget () and dopost (). httpservlet classes provide the doget () method to process GET requests. And provides the dopost () method to process post requests.
-> Doget (): The GET request sent by the client is called by the server. Use the Service () method of the genericservlet class to call this method. Rewrite get
The method also supports HTTP
Head request, which returns a response with no subject and only the title field. Before submitting a response, the servlet container needs to write the title. This is because in HTTP, the cursor must be sent before the response body is sent.
Question. The get method must be safe. If the client requests to change the stored data, other HTTP methods must be used. If the request format is incorrect, the doget () method returns the HTTP
Request error message. The syntax of the doget () method is:
Public void doget (httpservletrequest reqeust, httpservletresponse response) throws servletexception, ioexception
Request is the httpservletrequest object that stores client requests. Response is an httpservletresponse object that contains the server's response to the client.
-> Dopost (): it is called by the server to allow the servlet to process post requests sent by the client. Use the Service () method of the genericservlet class
Call this method. HTTP
The post method is used to send a large amount of data through internate. Before submitting a response, the servlet container needs to write the title. This is because in HTTP, the cursor must be sent before the actual response is sent.
Question. If the format of the http post request is incorrect, the dopost () method returns the HTTP "request error" message. Dopost () method Syntax:
Public void dopost (httpservletrequest request, httpservetresponse response) throws servletexception, ioexception
Specifically, request is the httpservletrequest object that stores client requests; response is the httpservletresponse object that contains the server's response to the client.

Servlet Lifecycle


The servlet lifecycle is controlled by the servlet container, which creates the servlet instance. The servlet Life Cycle refers to the response of the servlet instance after it is created.
The entire process from user request to destruction. The creation of the servlet instance depends on the first call of the servlet. The servlet interface defines three methods of the servlet lifecycle. Separately
As shown in the following figure:

-> Init () creates a servlet instance and initializes it. Objects that implement the servletconfig interface are passed as parameters. During initialization
The servlet container uses the servletconfig interface
The name of the initialization parameter, the value of the initialization parameter, and the name of the servlet instance. the syntax of the init () method is as follows:
Publci void Init (servletcofig config) throws servletexception
Config is the implementation that is passed to the init () method as a parameter. The object of the servletconfig interface.

-> Service (): responds to the request sent by the client. The service method receives the objects of the servletrequest interface and the servletrequest interface to process requests and send responses. The syntax of the service () method is:
Public void Service (servletrequest request, servetresponse response) throws servletexception, ioexception
Specifically, request is the object that is passed as a parameter to store the servletrequest interface requested by the client; response is the object of the servletresponse interface, which contains the servlet response.

-> Destroy (): If no request needs to be processed, release the servlet instance. The syntax of the destroy () method is:
Public void destroy ()
Each stage 1 of the servlet lifecycle is shown in.

The phases are described as follows:
-> Instantiation: The servlet container creates the Instance Object of the servlet class.
-> Initialization: The servlet container calls the servlet Init () method and usually applies for resources.
-> Service: the service is used by the container to respond to the customer's request to the servlet.
-> Destruction: resources are usually released when called before the servlet instance is released.
-> Unavailable: Release instances in memory.

To instantiate a servlet, the container must first find the Serlvet class, load the servlet class, and create the Serlvet object. Then, by calling the Servlet's
The init () method to initialize the servlet. The servletconfig interface object is passed as a parameter to the init () method. This interface object provides
Access to the servletconfig interface. The sevlet container uses the servletcontext interface to communicate with the servlet container.

If servlet initialization fails, an unavailableexception is thrown.
Or servletexception is abnormal. And then try to instantiate and initialize the servlet again. Then, set servletrequest and
The servletresponse interface object is passed as a parameter to the Service () method, which processes the request and returns a response.

If an exception is thrown in response to the request, the container detaches the instance by calling the servlet destroy () method. After the destroy () method is called, whether
The servlet instance and destroy method give the servlet the opportunity to clear resources (such as memory, file processing, and threads) to ensure all the statuses and
The current status of the servlet is synchronized.

Call of servlet Life Cycle methods:
Package test;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. util .*;

Public class testservlet extends httpservlet {
Private Static final string content_type = "text/html; charset = GBK ";
Int count;

// Read initialization parameters

Public void Init (servletconfig config) throws servletexception {
Super. INIT (config );
String initial = config. getinitparameter ("initial ");
Try {
Count = integer. parseint (initial );
} Catch (numberformatexception e ){
Count = 0;
}
System. Out. println ("the counter servlet has been successfully initialized ");
}
// Process http get requests
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Response. setcontenttype (content_type );
Printwriter out = response. getwriter ();
Out. println ("<HTML> ");
Out. println ("Out. println ("<body bgcolor =/" # ffffff/"> ");
Count ++;
Out. println ("since loading (this servlet has been accessed after the initialization parameter is read)" + Count + "times. ");
Out. println ("</body> ");
Out. println ("Out. Close ();
System. Out. println ("this servlet's doget method is executed once ");
}

// Process the http post request

Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}

// Clear Resources
Public void destroy ()
{
System. Out. println ("the counter servlet has released resources ");
}

}

Add the configuration items of the servlet to the Web. xml file.
Web. xml
-----------------------------------------
<Servlet>
<Servlet-Name> test </servlet-Name>
<Servlet-class>
Test. testservlet
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> test </servlet-Name> // name test here. When we access test, we use test to access the servlet test. testservlet.
<URL-pattern>/* </url-pattern>
</Servlet-mapping>
--------------------------------------------------

Start Tomcat and enter the name (TEST) defined in http: // localhost: 8080/project name/Web. XML in the address bar)

Console output information.

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.