Introduction to Request Header

Source: Internet
Author: User
When a client (usually a browser) sends a request to the Web server, it sends a requested command line, typically a GET or post command, and when the post command is sent, it must also send a request header called "Content-length" to the server (requested Header) to indicate the length of the requested data, it can send some other headers to the server, in addition to content-length, such as:

Accept the MIME type acceptable to the browser
Accept-charset Browser-supported character encoding
The Accept-encoding browser knows how to decode a data encoding type (such as gzip). Servlets can check if the browser supports gzip and can return gzipped HTML pages to browsers that support gzip, and set content-encoding response header (response header) To indicate that the content sent is already gzipped. In most cases, this can speed up the download of a Web page.
Accept-language the language specified by the browser, which works when the server supports multiple languages.
Authorization authentication information is generally a response to the Www-authenticate header issued by the server.
Connection whether to use persistent connections. If the servlet finds that the value of this field is keep-alive, or if the requesting command line discovers that the browser supports HTTP 1.1 (persistent connection is its default option), using persistent connections can reduce the download time for pages that protect many small files.
Content-length (the number of bytes to pass the data when submitting using the Post method)
Cookie (a very important header that is used for cookies-related operations, detailed information will be presented in the tutorial later)
Host (host and Port)
If-modified-since (returns only a new document that is newer than the specified date, and if not, will back 304 "not Modified")
Referer (URL)
User-agent (type of client, typically used to differentiate between different browsers)
If you want to learn more about the contents of the request header, you can visit the Web site of the consortium.

Read the contents of the request header in the servlet

It is easy to read the value of the request header in the servlet, as long as you call the HttpServletRequest GetHeader method, and when you specify the name of the header to return, The method returns the contents of the header of type string, or null if the specified header does not exist. Call Getheadernames can return enumeration that contains all header names.

A servlet program that reads all request header values

The following is an example of the request header in the servlet example that is shown in Tomcat and downloads the program.

Import java.io.*;
Import java.util.*;
Import javax.servlet.*;
Import javax.servlet.http.*;

public class Requestheaderexample extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception
{
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Enumeration E = Request.getheadernames ();
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Request.getheader (name);
OUT.PRINTLN (name + "=" + value);
}
}
}
Related Article

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.