1.Httpservlet class HTTPSERVLET class is an abstract class that can derive a subclass from that class to implement an HTTPSERVLET, accept requests from the Web site (this request comes from the client browser accessing the web site) and will process it The response result is sent back to the Web site (the Web site will send the response result to the client browser), in the subclass of the httpservlet, one of the following tables must be overloaded.
Method name
use
doget
If servlet supports HTTP GET requests for HTTP GET requests
Dopost
If servlet supports HTTP POST requests for HTTP POST requests
Doput
If servlet supports HTTP PUT requests for HTTP PUT requests
Dodelete
If servlet supports HTTP Delete requests for HTTP Delete requests
INIT and DESTROY
If you need to manage the resources held during the Servlet lifecycle, you can overload these two methods.
Typically, the Service method is not overloaded. For each HTTP request in the above table, the Service method handles these standard HTTP requests by assigning them to the corresponding Handler thread (Doxxx method).
Similarly, the DOOPTIONS and DOTRACE methods are often not overloaded, and the service method supports Http1.1 Trace and Options by assigning them to Dotrace and DoOptions.
Servlets are typically running in multi-threaded servers, so the written servlet code must be able to process parallel requests and synchronous access to data resources. Shared resources include data in memory (for example: instances or class variables) and external objects (for example: files, database connection, or network connections).
Protected Void Doget (HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RESP)
Throws ServleTexception, IOException
From the Service method to process the HTTP GET request. The GET method allows customers to read information from the web server, and the customer can tell the server what information it needs by passing a query string with a URL.
The DOGET method that supports the GET request will also automatically support the HTTP HEAD request. The HEAD request is also a GET request. It has only one request header field in the returned response without responding to the content of the information.
If the overload method, you should read the data from the request, set the entire headers, access the PrintWriter or the output stream object in the response, and finally write response data. When setting Headers, make sure containing Content Type and Encoding. If you use the PrintWriter object to return response, you must set the Content Type before accessing the PrintWriter object.
The servlet engine must write Headers before writing to write, because Headers may be refreshed at any time after writing data.
If you set the Content-Length header, the servlet can use a persistently stable connection to return its response to the client, dynamically improve performance.
If the format of the request is incorrect, the Doget method will return an HTTP BAD_REQUEST information.
The parameter REQ is an HTTPServletRequest object that contains the customer's servlet request; Resp is an HTTPSERVLETRESPONSE object that contains the response to the customer with the servlet.
Protected long getlastmodified (httpservletRequest Req) gets the last modification of the HTTPSERVLETRUEST object, since the number of milliseconds since January 1, 1970, if time does not know, return a negative number. The parameter REQ is an HTTPServletRequest object sent to the servlet. The return value is a long integer, which is used to refer to the last modification time to describe the HTTPSERVLETREQUEST object. The servlet engine that supports the HTTP GET request should be overloaded to provide a precise object modification time, which makes the cache work of the browser and proxy server, reducing the loading of the server and network resources. Protected Void Dopost (HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RESP) THROWS ServleTexeceptin, IOException accepts and handles HTTP POST requests from the service method. The HTTP POST allows the customer to send the length of data that is not limited to the web server, which is very useful when sending information such as a credit card number. If this method is to be overloaded, you should read the data, set the response headers (including Content-type and content-encoding), then access the Princewriter or the output stream object, and finally write response data using the javax.servlet.servletoutputStream object. If you use the PrintWriter object write response data, set the content-type before accessing the Princer object, the servlet engine must write Headers before responding to data, because the headers may be refreshed at any time after the Servlet engine starts writing. If you use HTTP1.1 block encoding (mean that there is a TRANSFER-ENCODing header), you do not need to set content-length header, otherwise, you should set the length of the content to allow servlet to use HTTP's "Connection Keep Alive" feature. If the format of the HTTP POST request is incorrect, the dopost method will return an HTTP BAD_AREQUEST information. The parameter REQ is an HTTPServletRequest object that contains the customer's ServelT request. Resp is an HttpservletResonse object that includes a response to the customer with a servlet. (Not finished, to be continued.)