Servlet Server HTTP Response

Source: Internet
Author: User
Tags session id

Servlet Server HTTP response

As discussed in the previous chapters, when a WEB server responds to an HTTP request, the response typically includes a status line, some response headers, a blank line, and a document. A typical response is as follows:

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 table summarizes the most useful HTTP 1.1 response headers that are returned to the browser from the Web server side, which you will use frequently in Web programming:

Header Information Description
Allow This header specifies the requested method (GET, POST, and so on) that the server supports.
Cache-control This header information specifies the circumstances under which the response document can be safely cached. Possible values are: public, Private, or no-cache , and so on. Public means that documents are cacheable, private means that documents are private documents for individual users and can only be stored in private (non-shared) caches, no-cache means that documents should not be cached.
Connection This header information indicates whether the browser uses a persistent HTTP connection. A value of close indicates that the browser does not use persistent HTTP connections, and the value keep-alive means using persistent connections.
Content-disposition This header information allows you to request that the browser require the user to save the response to disk with a file of the given name.
Content-encoding This header information specifies how the page is encoded during the transfer process.
Content-language This header information represents the language in which the document was written. For example, en, en-us, RU, etc.
Content-length This header information indicates the number of bytes in the response. This information is only required if the browser is using a persistent (keep-alive) HTTP connection.
Content-type This header information provides the MIME (multipurpose Internet Mail Extension) Type of the response document.
Expires This header information specifies when the content expires, after which the content is no longer cached.
Last-modified This header information indicates the last modification time of the document. The client can then cache the file and provide a date through the If-modified-since request header information in a later request.
Location This header information should be included in all responses with a status code. Within 300s, this notifies the address of the browser document. The browser will automatically reconnect to this location and get a new document.
Refresh This header information specifies how the browser should request an updated page as soon as possible. You can specify the number of seconds to refresh the page.
Retry-after This header information can be used in conjunction with the 503 (Service Unavailable services unavailable) response, which tells the client how long it will be possible to repeat its request.
Set-cookie This header information specifies a cookie associated with the page.

Methods for setting HTTP response headers

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

Serial Number Method & Description
1 The string encoderedirecturl (string url) encodes the specified URL used in the Sendredirect method, or if the encoding is not required, the return URL is unchanged.
2 The string encodeurl (string url) encodes the specified URL that contains the session ID, or if the encoding is not required, the return URL does not change.
3 boolean Containsheader (String name) Returns a Boolean value that indicates whether the named response header has been set.
4 boolean iscommitted () returns a Boolean value indicating whether the response has been committed.
5 void Addcookie (Cookie cookie) adds the specified cookie to the response.
6 void Adddateheader (String name, long date) adds a response header with the given name and date value.
7 void AddHeader (string name, string value) adds a response header with the given name and value.
8 void Addintheader (String name, int value) adds a response header with the given name and integer value.
9 void Flushbuffer () forces any content in the buffer to be written to the client.
10 void Reset () clears any data that exists in the buffer, including the status code and header.
11 void Resetbuffer () clears the contents of the underlying buffer in the response, and does not clear the status code and header.
12 void senderror (int sc) sends an error response to the client using the specified status code, and clears the buffer.
13 void Senderror (int sc, String msg) sends an error response to the client using the specified state.
14 The void Sendredirect (String location) sends a temporary REDIRECT response to the client using the specified redirect position URL.
15 void setbuffersize (int size) sets the preferred buffer size for the response body.
16 void setcharacterencoding (String charset) sets the character encoding (MIME character set) of the response that is sent to the client, for example, UTF-8.
17 void setcontentlength (int len) sets the length of the content body in the HTTP Servlet response, and the method sets the HTTP Content-length header.
18 void setContentType (String type) sets the content type of the response that is sent to the client if the response has not yet been committed.
19 void Setdateheader (String name, long date) sets a response header with the given name and date value.
20 void SetHeader (string name, string value) sets a response header with the given name and value.
21st void Setintheader (String name, int value) sets a response header with the given name and integer value.
22 void SetLocale (Locale loc) sets the region of the response if the response has not yet been committed.
23 void setStatus (int sc) sets the status code for the response.

HTTP Header Response Instance

You have seen the setContentType () method in the previous instance, the same method is used in the following instance, and we will use the setintheader () method to set the Refresh head.

serverresponse.java

Package Hentai.servlet;import Java.io.ioexception;import Java.io.printwriter;import Java.text.SimpleDateFormat; Import Java.util.calendar;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 Serverresponse */@WebServlet ("/           Serverresponse ") public class Serverresponse extends HttpServlet {private static final long serialversionuid = 1L;        /** * @see httpservlet#httpservlet () */public Serverresponse () {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//Set the response header Response.setcontenttype ("Text/html;charset=utf-8"); Response.setintheader("Refresh", 3);//3 seconds after refresh Response.setintheader ("Refresh", 1);//output response content PrintWriter out = Response.getwriter (); String document = "<! Doctype> "+" "+" 

Servlet Server HTTP Response

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.