When looking at the following text, first we need to clarify the figure above, so that different classes will be inherited according to different requirements during encoding.
The core of servlet APIS is javax. servlet. servlet interface, all Servlet classes must inherit this interface, five methods are defined in the servlet interface, three of which are called by the servlet container, containers call different methods at different stages of the servlet lifecycle.
@ Init (servletconfigconfig) method: initializes the servlet object. The container starts to call this method after it is created.
@ Service (servletrequestreq, servletresponse res) method: responds to customer requests. After the container receives a message from the client, it will call the servic method and then process it according to the get or POST method in the HTTP protocol and then use the doget () and dopost methods in the servlet.
@ Destroy () method: This method is used to release resources occupied by Servlet objects. This method is called when the servlet object ends its lifecycle.
When our servlet inherits the genericservlet class, it will automatically write the @ override of the parent class
Public voidService (servletrequest req, servletresponse res)
ThrowsServletexception, ioexception {
} Method.
The following describes the servletrequest interface in detail:
In the preceding method, there is a parameter of the servletrequest type. The servletrequest class indicates a request from the client. When the servlet container receives the request from the client to access a specific servlet, the container first parses the original data of the client and packs it into a servletrequest object. When the container calls the service method of the servlet object, it can pass servletrequest as a parameter to the Service () method.
The servletrequest interface provides a series of methods to read client request data.
@ Getcontentlength (): returns the length of the Request body. If the length of the Request body is unknown,-1 is returned;
@ Getcontenttype (): Obtain the MIME type of the Request body. If the type of the Request body is unknown, null is returned.
@ Getinputstream (): The input stream used to read the Request body
@ Getlocaladdr (): returns the IP address of the server.
@ Getlocalname (): returns the Host Name of the server.
@ Getlocalport (): return the ftp port number on the server.
@ Getparameter (string name): return the value of the request's Sino-German matching request parameter based on the given parameter name.
@ Getprotocol (): return the protocol name and version number used for communication between the client and the server.
@ Getreader (): return the bufferreader object used to read the Request body in string format.
@ Getremoteaddr (): returns the IP address of the client.
@ Getremotehost (): returns the host name of the client.
@ Getremoteport (): return the ftp port number of the client.
In addition, the servletrequest interface defines a set of methods for retrieving shared data in the request range memory.
@ Setattribute (string name, object): stores an attribute value within the request range. The parameter name indicates the attribute name, and the parameter object indicates the attribute value.
@ Getattribute (string name): returns the value of a matched attribute in the request range based on the attribute name specified by the name parameter.
@ Removeattribute (string name): deletes an attribute value from the request range.
The following describes the servletresponse interface in detail:
The servletresponse interface defines a series of methods related to generating response results.
@ Setcharacterenocoding (string charset) sets the character encoding for the response body, which is the iso-8859-1 by default
@ Setcontentlength (INT Len) set the length of the response body
@ Setcontenttype (string type): sets the MIME type of the response body.
@ Getcharacterencoding (): returns the character encoding of the response body.
@ Getcontenttype () the MIME type of the response body returned
@ Setbuffersize (INT size): Set the buffer size used to store response body data.
@ Getbuffersize (): obtains the buffer size used to store response body data.
@ Reset (): clears the body data of the buffer, and clears the response status code and response header.
@ Resetbuffer (): Only clears the body data of the buffer, and does not clear the response status code and response header.
@ Flushbuffer (): forcibly sends the response body of the buffer zone data to the client.
@ Iscommited (): returns a Boolean value. "True" indicates that data is buffered and submitted to the client. The opposite is true.
@ Getoutputstream (): returns a servletoutputstream object that is used to output binary data.
@ Getwriter (): returns a printwriter object, which is used by the servlet to output data in the string format.
After inheriting httpservlet, the following methods of the parent class will be rewritten:
Public class Setvlert ExtendsHttpservlet {
PublicSetvlert (){
Super();
}
Public voidDestroy (){
Super. Destroy (); // just puts "Destroy" string in log
}
Public voidDoget (httpservletrequest request, httpservletresponse response)
ThrowsServletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out
. Println ("<! Doctype HTML public \ "-// W3C // DTD html4.01 transitional // en \"> ");
}
Public voidDopost (httpservletrequest request, httpservletresponse response)
ThrowsServletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
}
Public voidInit ()ThrowsServletexception {
}
}
The httpservletrequest interface is a sub-interface of the servletrequest interface.The httpservletrequest interface provides information for reading HTTP requests.
@ Getscheme () method returns the request plan, such as HTTP, https, or FTP.
@ Getservername () method returns the Host Name of the server sending the request
@ Getserverport () method returns the port number of the sent request.
@ Getcontextpath () returns the root directory of the request address, which is switched to/, but not ended.
A commonly used connection string for obtaining the server address is:
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
@ Getcookies () get Cookie
@ Getmethod (): Obtain the request method, such as get, post, or put.
@ Getrequesturl () Get the request URL (Uniform Resource Locator)
@ Getrequesturi () Get the request URI (Uniform Resource Identifier)
@ Getsession () Get the corresponding session
@ Getheadernames () returns an enumeration (traversal) of all header names contained in the request)
The httpservletreponse interface provides information for setting HTTP requests.
@ The servlet can use the getwriter method to obtain the printwriter object and output the character data. The servlet can use the getoutputstream method to obtain the servletoutputstream object. Servletoutputstream can output both character data and binary data in MIME format. If the getwriter method has been used, java. Lang. illegalstateexception will be thrown when getoutputstream is used.
@ Setcontenttype: The content format and length can be indicated in the response.
@ Setbuffersize method: sets the buffer size of the Web container. The getbuffersize method returns the buffer size of the Web container. The resetbuffer method clears and resets the buffer, and the reset method is used to clear the buffer and status header information, use the flushbuffer method to transmit all the output content in the buffer to the customer. Use the iscommitted method to determine whether the response has been executed. "Fulfillment" is defined as the status code that has been written into the response.
The @ setlocal method sets the region information of the response, which is mainly used when the international currency of the web program is internationalized. This method should be used before getwriter. The region information of the default web server.
Httpservletresponse inherits the servletresponse interface and provides methods related to the HTTP protocol. The main functions of these methods are to set HTTP status codes and manage cookies.
The @ setstatus method is used to set the status code returned by the servlet to the client. It is used to set the status without errors. If the servlet fails to run, the servlet can use the senderror method to set the status code, for example, the senderror (int SC) method to set the error status code. The senderror (int SC, string MSG) method sends an error message to the customer in addition to setting the status code.
@ Setheader: You can specify the header information value of a specific name. adddateheader, addintheader, setdateheader, and setintheader can be added to or rationally specified time or integer header information.
@ Addcookie: the cookie object can be added to the web server response. This object will be saved by the browser. The cookie mechanism is also used to maintain the session status.
@ Sendredirect: The method sends a temporary redirection response to the customer. It generates a 302 response status code. The response gives the customer a new URL. If the buffer has been cleared, this method will bring up an illegalstateexception.