Seven, HTTP response status

zhaozj2021-02-16  161

7.1 Status Code Overview When a web server responds to a browser or other client request, its response is generally consisting of the following parts: a status line, several responses, one blank, content document. Below is a simplest answer: http / 1.1 200 okcontent-type: text / plainhello World status row contains a short description information corresponding to the HTTP version, status code, and status code. In most cases, all responses other than Content-Type are optional. However, Content-Type is required, which describes the MIME type of the back document. Although most responses include a document, there are also some do not include, such as responding to the HEAD request never comes with a document. There are many status code actually used to identify a request for failure, which does not contain documents (or only a short error message description). Servlets can utilize status code to implement many functions. For example, you can redirect the user to another; you can indicate that the following document is a picture, a PDF file, or an HTML file; you can tell the user to provide a password to access documents; and so on. In this part we will specifically discuss the meaning of various status code and how to use these code. 7.2 Set status code As previously mentioned, the HTTP response status row contains HTTP versions, status code, and corresponding status information. Due to the status information is directly related to the status code, the HTTP version is determined by the server, so there is only one status code for servlet settings. Servlet Setting Status Codes Generally uses HTTPSERVLETRESPONSE's SetStatus method. The parameter of the setStatus method is an integer (ie, status code), but in order to make the code have better readability, it can be used to avoid direct use of integers in HTTPSERVLETRESPONSE. These constants are named in accordance with the standard status information in HTTP 1.1, and all the names add the SC prefix (STATUS CODE abbreviation) and uppercase, while converting spaces into underscores. That is, the status information corresponding to the status code 404 is "not found", and the corresponding constant name in HTTPSERVLETRESPONSE is SC_NOT_FOUND. However, there are two exceptions: The constant corresponding to the status code 302 is named by HTTP 1.0, and 307 has no corresponding constant. Setting status code does not always mean not return documents. For example, although most servers returns 404 responses, simply "File Not Found" information is output, but servlets can also customize this response. However, the custom response should call Response.SetStatus before sending anything via PrintWriter. Although setting status code is typically used is a response.setstauts (int) method, HTTPSERVLETRESPONSE provides a dedicated method for both common situations: SendError method generates a 404 response while generating a short HTML error message document; The SendRedirect method generates a 302 response while indicating the URL of the new document in the local header. 7.3 HTTP 1.1 Status Code and Its Meaning The following table shows the common HTTP 1.1 status code and their corresponding status information and meaning. State code supported by HTTP 1.1 should be carefully used, because many browsers can only support HTTP 1.0. If you use the HTTP 1.1 unique state code, it is best to check the requesting HTTP version number (through the GetProtocol method of HttpservletRequest).

Status Code Status Information Meaning 100 Continue The initial request has been accepted, and the customer should continue to send the rest of the request. (HTTP 1.1 new) 101 Switching Protocols server will follow the customer's request to another protocol (HTTP 1.1 new) 200 ok everything is normal, the answer document requesting the GET and POST requests back. If you don't have to set status code, the servlet uses the 202 status code by default. The 201 CREATED server has created a document, and the Location header gives its URL. 202 ACCEPTED has been accepted, but the processing has not been completed. 203 Non-Authoritative Information The document has returned normally, but some responses may not be correct because the document is used (HTTP 1.1 new). 204 no content does not have a new document, the browser should continue to display the original document. If the user regularly refreshes the page, the servlet can determine that the user document is new enough, and this status code is useful. 205 Reset Content has no new content, but the browser should reset the content it displayed. Used to force the browser to clear the form input content (HTTP 1.1 new). 206 Partial Content The customer sent a GET request with the Range header, which completed it (HTTP 1.1 new). 300 MULTIPLE Choices Customer Requests Documents can be found at multiple locations, which have been listed within the returned document. If the server wants to make a priority, you should indicate at the Location response. 301 MOVED Permanently Customer Request Document In other places, the new URL is given in the location header, and the browser should automatically access the new URL. 302 FOUND is similar to 301, but the new URL should be considered a temporary alternative, not permanent. Note that the corresponding status information in HTTP1.0 is "Moved Temporatily", and the corresponding constant in httpservletResponse is SC_MOVED_TEMPORARILY instead of sc_found. When this status code appears, the browser automatically accesses the new URL, so it is a very useful status code. To do this, servlet provides a dedicated method, namely SendRedirect. It is better to use Response.sendredirect (URL) than using response.setstatus (response.sc_moved_temporarily) and response.setheader ("Location", URL). This is because: First, the code is more concise. Second, use SendRedirect, servlet automatically construct a page containing new links (for old browsers that cannot be redirected). Finally, SendRedirect can handle relative URLs, automatically convert them into absolute URLs. Note that this status code is sometimes used for replacement with 301. For example, if the browser is incorrectly requests http: // Host / ~ User (missing behind the late slash), some servers return 301, and some returns 302. Strictly speaking, we can only assume that the browser will automatically redirect when the original request is Get. See 307.

303 SEE Other is similar to 301/302, and the difference is that if the original request is POST, the redirect target document specified by the local header should be extracted (HTTP 1.1 new). 304 NOT MODIFIED The client has buffered documents and issues a conditional request (generally providing the IF-Modified-Since header indicating that the customer only updates the document updated than the specified date). The server tells customers that the original buffer document can also be used. 305 USE Proxy Customer Request documents should be extracted by the proxy server indicated by the Location header (HTTP 1.1 new). 307 Temporary Redirect and 302 (Found) are the same. Many browsers are incorrectly responding to the 302 response to redirect, even if the original request is POST, even if it actually only responds to the POST request is 303, it can be redirected. For this reason, HTTP 1.1 has increased by 307 to more clear the regional division of state code: When the 303 response occurs, the browser can follow the redirected GET and POST requests; if it is 307 response, the browser can only follow Redirection to the GET request. Note that there is no corresponding constant for the status code in HTTPSERVLETRESPONSE. (HTTP 1.1 new) 400 Bad Request requests a syntax error. 401 Unauthorized Customers attempt to access the password-protected page without authorization. A WWW-Authenticate header will contain a www-authenticate header, the browser displays the user's name / password dialog, and then issues a request after filling the appropriate Authorization header. 403 Forbidden resource is not available. The server understands the customer's request, but refuses to handle it. It is usually caused due to the permissions settings of the file or directory on the server. 404 NOT FOUND Unable to find resources for the specified location. This is also a common response, HTTPSERVLETRESPONSE provides the corresponding method: Senderror (Message). 405 Method Not ALLOWED Request Method (GET, POST, HEAD, DELETE, PUT, TRAC, etc.) do not apply to the specified resource. (HTTP 1.1 new) 406 Not Acceptable Specifies the resource specified, but its MIME type and customer are not compatible (new HTTP 1.1) specified in the ACCPET header. 407 Proxy Authentication Required Similar to 401, the customer must first authorize the proxy server. (HTTP 1.1 new) 408 Request Timeout In the waiting time of the server license, customers have not issued any requests. Customers can repeat the same request later. (HTTP 1.1 new) 409 Conflict is usually related to the PUT request. Since the request and the current state of the resource conflict, the request cannot be successful. (HTTP 1.1 new) 410 Gone requested documentation is no longer available, and the server does not know which address should be redirected. The difference between it and 404 is that the return 407 indicates that the document is permanently left the specified location, and 404 indicates that the document is not available for unknown reason. (HTTP 1.1 new) 411 Length Required server cannot handle the request unless the customer sends a Content-Length header.

(HTTP 1.1 new) 412 Precondition Failed requests some prerequisites fails (HTTP 1.1 new). 413 Request Entity Too Large The size of the target document exceeds the size of the server is currently willing to handle. If the server considers yourself to process the request later, you should provide a Retry -After header (HTTP 1.1 new). 414 Request Uri Too Long Uri is too long (HTTP 1.1 new). 416 Requested Range Not Satisfiable server does not satisfy the Range header specified in the request. (HTTP 1.1 new) 500 INTERNAL Server Error server encountered unexpected situations and cannot complete customer requests. The 501 Not Implemented server does not support the functions required to implement the request. For example, the customer issued a PUT request that the server is not supported. 502 BAD GATEWAY Server As a gateway or agent, in order to complete the request to access the next server, the server returns an illegal response. 503 Service UNAVAILALALABLE server has failed to respond due to maintenance or load. For example, servlet may return 503 in the case where the database connection pool is full. A Retry -After header can be provided when the server returns 503. 504 GATEWAY TIMEOUT is used by a server as a proxy or gateway, indicating that you cannot get a response from the remote server in time. (HTTP 1.1 new) 505 HTTP Version Not Supported Server does not support the HTTP version indicated in the request. (HTTP 1.1 new) 7.4 Instance: Accessing the plurality of search engines This example uses two other common status code except 200: 302 and 404.302 Set by SendRedirect method, 404 is set by Senderror method. In this example, the first HTML form appears to select the search engine, search the string, and the number of search results per page. After the form is submitted, the servlet extracts these three variables, and the URL containing these variables is constructed according to the requirements of the selected search engine, and then redirect the user to this URL. If the user does not correctly select the search engine, or use other forms to send an unknown search engine name, returns a 404 page that the search engine is not found. Searchengines.java Note: This servlet is to use the SearchSpec class given later, and the SearchSpec's function is to construct the URL that is suitable for different search engines. package hall; import java.io *;. import javax.servlet *;. import javax.servlet.http *;. import java.net *;. public class SearchEngines extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// getParameter automatically decodes the URL encoded query string.

Since we // want to send the query string to another server, use // Urlencoder to use // Urlencoder to perform URL encoded SearchString = urlencoder.Encode ("searchstring")); string NumResults = request.getParameter ("NumResults "); String searchEngine = request.getParameter (" searchEngine "); SearchSpec [] commonSpecs = SearchSpec.getCommonSpecs (); for (int i = 0; i

; If (searchSpec.getName () equals (searchEngine).) {String url = response.encodeURL (searchSpec.makeURL (searchString, numResults)); response.sendRedirect (url); return;}} response.sendError (response.SC_NOT_FOUND , "No recognized search engine specified.");} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response);}} SearchSpec.java package hall; class SearchSpec {private String name, baseURL , NumResultssuffix; private static searchspec [] commitpecs = {new searchspec ("Google", "http://www.google.com/search?q=", "& num ="), New SearchSpec ("Infoseek", "HTTP : //infoseek.go.com/titles? qt = "," & nh = "), New SearchSpec (" Lycos "," http://lycospro.lycos.com/cgi-bin/pursuit?query = "," & Maxhits = "), New searchspec (" Hotbot "," http://www.hotbot.com/?mt= "," & DC = ")}; public searchspec (String Name, String Baseurl, String NumResultssuffix ) {this.name = name; this.baseURL = baseURL; this.numResultsSuffix = numResultsSuffix;} public String makeURL (String searchString, String numResults) {return (baseURL searchString numResultsSuffix numResults);} public String getName () { Return (Name);

转载请注明原文地址:https://www.9cbs.com/read-8985.html

New Post(0)