Servlet HTTP status code and get browser URL

Source: Internet
Author: User

Servlet HTTP Status code

The format of the HTTP request and HTTP response message is similar, with the following structure:

  • Initial status line + carriage return newline character (carriage return + line feed)
  • 0 or more header lines + carriage return line break
  • A blank line, the carriage return line break
  • An optional message body, such as file, query data, or query output

For example, the server's response header looks like this:

http/1.1 okcontent-type:text/htmlheader2: ... HeaderN:  ... (Blank line) <!doctype ... >

The status line includes the HTTP version (in this case, http/1.1), a status code (200 in this example), and a short message that corresponds to the status code (OK in this case).

The following is a list of HTTP status codes and related information that may be returned from the WEB server:

Code message Description
100 Continue Only part of the request has been received by the server, but as long as it is not rejected, the client should continue the request.
101 Switching protocols Server switching protocol.
200 Ok The request was successful.
201 Created The request is complete, and a new resource is created.
202 Accepted The request is accepted for processing, but the processing is incomplete.
203 Non-authoritative Information
204 No Content
205 Reset Content
206 Partial Content
300 Multiple Choices A list of links. The user can select a link to go to that location. Maximum of five addresses.
301 Moved Permanently The requested page has been transferred to a new URL.
60W Found The requested page has been temporarily transferred to a new URL.
303 See other The requested page can be found under another different URL.
304 Not Modified
305 Use Proxy
306 Unused Use this code in a previous version. It is no longer used, but the code is still preserved.
307 Temporary Redirect The requested page has been temporarily transferred to a new URL.
400 Bad Request The server does not understand the request.
401 Unauthorized The requested page requires a user name and password.
402 Payment Required You cannot use this code yet.
403 Forbidden Access to the requested page is forbidden.
50U Not Found The server was unable to find the requested page.
405 Method not allowed The method specified in the request is not allowed.
50W Not acceptable The server only generates a response that is not accepted by the client.
407 Proxy Authentication Required You must use Proxy server authentication before requesting delivery.
408 Request Timeout The request takes longer than the server can wait to time out.
409 Conflict The request could not be completed because of a conflict.
410 Gone The requested page is no longer available.
411 Length Required "Content-length" is undefined. The server cannot process request information sent by the client without content-length.
412 Precondition Failed The prerequisites given in the request are evaluated by the server as false.
413 Request Entity Too Large The server does not accept the request because the request entity is too large.
414 Request-url Too Long The server does not accept the request because the URL is too long. Occurs when you convert a "post" request to a "get" request with long query information.
415 Unsupported Media Type The server does not accept the request because the media type is not supported.
417 Expectation Failed
500 Internal Server Error The request was not completed. The server encountered an unexpected situation.
501 Not implemented The request was not completed. The server does not support the required functionality.
502 Bad Gateway The request was not completed. The server received an invalid response from the upstream server.
503 Service unavailable The request was not completed. The server is temporarily overloaded or crashed.
504 Gateway Timeout The gateway timed out.
505 HTTP Version not supported The server does not support the "HTTP protocol" version.

How to set the HTTP status code

The following methods can be used to set the HTTP status code in a Servlet program. These methods are available through the httpservletresponse object.

Serial Number Method & Description
1 Public void SetStatus (int statusCode) This method sets an arbitrary status code. The SetStatus method takes an int (status code) as a parameter. If your response contains a special status code and documentation, make sure to call SetStatus before using PrintWriter to actually return any content.
2 Public void Sendredirect (String URL) The method generates a 302 response, along with a location header with a new document URL .
3 Public void Senderror (int code, String message) This method sends a status code (typically 404), along with a short message that is automatically formatted and sent to the client inside the HTML document.

HTTP status code and get URL instance

The following example notes the code that sends the 407 error and sends the redirect, which is used to output the obtained URL information.

Statuscode.java

Package Hentai.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation Class StatusCode */@WebServlet ("/statuscode") public class StatusCode extends HttpServlet {private static final long seria           Lversionuid = 1L;        /** * @see httpservlet#httpservlet () */public StatusCode () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated Method Stub//response.senderror (407, "https://www.baidu.com/");// Response.sendredirect ("https://www.baidu.com/"); Response.getwriter (). Append ("Served at:"). Append ( Request.getcontextpath ()); RespoNse.getwriter (). Append ("<br/>url:"). Append (Request.getscheme () + "://" +request.getservername () + ":" + Request.getserverport () +request.getcontextpath () +request.getservletpath ()), Response.getwriter (). Append ("<br />requesturl: "). Append (Request.getrequesturl ()); Response.getwriter (). Append (" <br/> "+ Request.getservletcontext (). toString ()); Response.getwriter (). Flush (); /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated Method Stubdoget (Request, Response);}}

Servlet HTTP status code and get browser URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.