Servlet has been told by the following method in the previous section:
Realize the service method. Implement the Domethod method of HTTPSERVLET (Doget, Dodelete, DOOPTIONS, DOPOST, DOPUT, DOTRACE). Typically, the Service method is used to extract information from the client request, access the extended resource, and provide a response based on the above information. For HTTP Servlets, the process that correctly provides a response is first fill in the response (Response) header information, then gets the output stream from the response (response), and finally writes content information in the output stream. Response (response) header information must be set first. How to get information and generate HTTP responses (Response) will be described below in request. Get the client request an HTTPSERVLETREQUEST object to provide the HTTP header data and allows you to get the client's data. How to get these data depends on the HTTP side request method. Regardless of any HTTP mode, you can return the parameter value of a specific name with the getParameterValues method. This getQueryString method will return a value that can be used to analyze with an HTTP GET request. The client request contains data that passes from the client to servlet. All requests implements a servletRequest interface. This interface defines some ways to access the following information, as shown in Table 14-1.
Table 14-1 ServletRequest Interface Method
Type description
Parameters, used to transfer information between client and servlet (String Name)
GetAttributeNames ()
GetInputStream ()
getParameter (String Name)
getParameterMap ()
getParameterNames ()
GetParameterValues (String Name)
Object Value Properties, used in servlet containers and servlet
Between, or the collaborative servlet transfer information Removeattribute (String Name)
SetAttribute (String Name, Object O)
Protocol information about requesting use,
Call getContentLength () in the request in the request
GetContentType ()
GetProtocol ()
GetReader ()
GetRealPath (String Path)
GetRemoteAddr ()
GetRemotehost ()
GetRequestDispatcher (String Path)
Protocol information about requesting use,
Call getScheme () in the request in the request
GetServerName ()
GetServerPort ()
ISecure ()
Information about localization getcharacterencoding ()
GetLocale ()
Getlocales ()
SetCharacterenceEnding (String Env)
The following code segment demonstrates how to use the method in the request to get client information.
Enumeration params = request.getParameterNames ();
String paramname = NULL;
String [] Paramvalues = NULL;
While (params.hasmorelements ()) {
Paramname = (string) params.nextelement ();
Paramvalues = Request.getParameterVale (paramname);
System.out.println ("/ NParameter Name IS" paramname);
For (int i = 0; i System.out.println (", Value" i "IS" paramvalues [i] .tostring ()); } } HTTP Servlets uses the HTTP Request object (HTTPSERVLETREQUEST), which contains the Request URL, HTTP header information, query strings, and more. HTTP Request URL includes several parts: http: // : ? Under normal circumstances: Requesturi = ContextPath ServletPath PathInfo Context path: Get it through the getContextPath method. Servlet Path: Get it through the GetServletPath method. PathInfo: obtained through the getPathInfo method. As shown in Table 14-2. Table 14-2 corresponds to the path Request Path Path Elements /catalog/help/feedback.jsp ContextPath: / catalog servletpath: /Help/Feedback.jsp Pathinfo: NULL The HTTP response response is provided (response) contains data transmitted between servers and clients. All responses have implemented a servletResponse interface. This interface defines that some methods are provided to developers, as shown in Table 14-3. Table 14-3 SERVLETRESPONSE interface method Type description The output stream to send data to the client is sent: getWriter () Send byte stream: getOutputStream () Indicates the type of content returned (for example: Text / HTML) The name of the already registered content is saved in IANA (Internet Assigned Numbers Authority) SetContentType (Java.lang.String Type) Indicates whether it is a buffer output. Write output by default The content is sent immediately to the client. Write the contents of the output after the buffer first Do not send to the client, so servlet has more time settings Status code and header information, or transfer to other web resources flushbuffer () GetBuffersize () iScommitted () RESET () ResetBuffer () SetBuffersize (int size) SetContentLength (int LEN) Set localization information getcharacterencoding () GetLocale () SetLocale (Java.util.Locale Loc) HTTP RESPSETRESPONSE has some domain representing HTTP header: Status code is used to indicate the response (response) failure. Cookies stores applications related to the client, sometimes cookies to maintain and identify the user's session. Servlet first sets the response (Response) header information, the content category of response, and the buffer size, and then gets the PrintWriter from the response (response) in the doget method, and write the HTML code in the output, call the close () method Submit this response to the client (response). The demonstration code is as follows: Public void doget (httpservletRequest Request, Httpservletresponse response Throws ServleTexception, IOException { // set the header information Response.setContentType (Text / HTML "); Response.setBuffersize (8192); PrintWriter out = response.getwriter (); / / Output to Response Out.println (""
Messages.getstring ("TitleBookDescription")
Title> head> ");
...
Out.println (" Body> HTML>");
// Turn off output flow
Out.close ();
}
Here is an example of an HTTP Servlet handling the post method (T111) This article is selected from the Freescale Book "Jing Java Core Technology"