As we can see earlier, requests from browsers are composed of one HTTP command (get or post), zero or more request headers, one blank line, and some query data (post requests:
GET/servlet/somename HTTP/1.1
HOST :...
Header2 :...
...
Headern:
(Blank line)
... (Query data)
The response of the Web server to the request is generally composed of a status line, some response headers, a blank line, and corresponding documents. The typical response form is as follows:
HTTP/1.1 200 OK (status line)
Content-Type: text/html
Header2 :...
...
Headern :...
(Blank line)
<! Doctype...> // output content
<HTML>
<Head>... <Body>
...
</Body>
A status line consists of an HTTP version, a status code (integer), and a short message.
Through the operation status line and response header, the servlet can execute multiple tasks. For example, they can forward users to other websites, indicate the types of additional documents, and inform users that a password is required to access this document; and so on.
6.1 specify the status code
1) set any status code setstatus
Use the setstatus method of httpservletresponse. The setstatus method uses integers as parameters. For readability, constants defined in httpservletresponse are generally used. All these constants are prefixed with SC, and their names correspond to standard HTTP messages.
2) set the status codes sendredirect and senderror for 302 and 404.
Httpservletresponse provides shortcuts for two common status codes,
Public void sendredirect (string URL)
Status Code 302 command the browser connects to a new location, this method generates a 302 response
Public void senderror (INT code, string message)
Status Code 404 is used when the server does not find a document. This method sends status code int code (generally 404) and a short message string message.
6.2 http1.1 status code
Understand and familiarize yourself with some of the most important status codes.
100-199
Information, marking other actions that the customer should take
200-299
Request successful
300-399
Used to indicate the new address for the files that have been removed
400-499
Indicates the error caused by the customer
500-599
Indicates the error caused by the server
In httpservletresponse, constants are generally used to represent the status code. The constant name shows the meaning of the State. For example, response. setstatus (response. SC _no_content) instead of response. setstatus (204 ).
Meanings of some common status codes,
6.3 Example: redirect a user to a related page
The following list reads the content of the header User-Agent and forwards the user to different pages based on different browser types.
Package coreservlets;
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class wrongdestination extends httpservlet {
Public void doget (httpservletrequest request,
Httpservletresponse response)
Throws servletexception, ioexception {
String useragent = request. getheader ("User-Agent ");
If (useragent! = NULL )&&
(Useragent. indexof ("MSIE ")! =-1) {// features of the User-Agent header of the IE user
Response. sendredirect ("http://home.netscape.com"); // redirect
} Else {
// Other browser users go to another page
Response. sendredirect ("http://www.microsoft.com ");
}
}
}
6.4 example: a front-end of various search engines (302 and 404 status code are used together)
This chapter focuses on understanding the server's response to the request, response format, status code and its meaning, how to use them, and these problems.