HTTP request first reading
Abstract: HTTP request first reading
HTTP requests outlined in the first 5.1
HTTP client program (such as browsers), sent to the server when the request must specify the type of request (generally GET or POST). If necessary, clients can also choose to send the request of other first. Most of the first request is not necessary, but except for Content-Length. POST request, the Content-Length must appear.
Below are some of the most common requests head:
Accept: browser acceptable MIME type.
Accept-Charset: browser acceptable character set.
Accept-Encoding: Browsers can decode the data coding methods, such as gzip. Servlet able to support gzip browser to return to the gzip encoding HTML pages. In many cases this can be reduced 5 to 10 times the download time.
Accept-Language: browser type of language that when the server can provide more than one language version to use.
Authorization: authorization information often appears in the server sent WWW-Authenticate response in the first.
Connection: that the need for persistent connections. Servlet here to see if the value of "Keep-Alive", or request that the use of HTTP 1.1 (HTTP 1.1 default lasting connection), it can make use of the advantages of persistent connections, when the page contains a number of elements, ( Applet For example, picture), significantly reducing the time needed to download. To achieve this, Servlet need, in response, sent a Content-Length first, the most simple method: the contents into ByteArrayOutputStream first, and then write the content before the formal calculation of its size.
Content-Length: request news that the length of the body.
Cookie: This is the most important information at the request of one of the first, see behind the "Cookie handling of the" chapter of the discussion.
From: request the email address of the sender, some special procedures, the use of the Web, the browser will not use it.
Host: URL in the initial host and port.
If-Modified-Since: only when the contents of the request after the date specified in the revised and will return to it, or return 304 "Not Modified" response.
Pragma: designated "no-cache" value that the server must return a refresh the document, even if it is a proxy server pages and have a local copy.
Referer: contains a URL, user representatives from the URL of the page to visit the requested page.
User-Agent: browser type, if Servlet return to the contents of the browser type and the value is very useful.
UA-Pixels, UA-Color, UA-OS, UA-CPU: from some versions of IE browser sent at the request of the non-standard first that the screen size, color depth, operating system and CPU type.
The HTTP header integrity, and detailed explanation, please refer to the HTTP http://www.w3.org/Protocols/ norms.
Servlet 5.2 in the first read request
HTTP Servlet read in the head is very convenient, only need to call the HttpServletRequest getHeader method can be. If the customer has specified in the request to provide the first information, getHeader return to the corresponding string Otherwise, the return null. Often use the first part of the information that they have dedicated access methods: Cookie getCookies method returns the contents of the first, after the analysis of the object stored in an array of Cookie, Cookie please see the sections behind the discussion getAuthType Reading Methods and getRemoteUser Authorization from the first part; getDateHeader and getIntHeader method specified in the first reading, then returned to the date value or integer values.
Apart from the first designated reader, can be used getHeaderNames also request all Enumeration first name of a target.
Finally, in addition to the first request Show information, we may also request the command line to receive some information. GetMethod method returns request, the request is usually GET or POST, but may also be HEAD, PUT or DELETE. GetRequestURI method returns URI (URI is a URL from the host and port to form after the data before that part). GetRequestProtocol request orders to return to the third part of the general "HTTP/1.0" or "HTTP/1.1."
5.3 Example: the first at the request of all output
Below the Servlet examples of all first received the request and its value in the form of a tabular output. In addition, the output Servlet will request an order of the three main parts: request method, URI, the agreement / version.
ShowRequestHeaders.java
Package hall;
Import java.io. *;
Import javax.servlet .*;
Import javax.servlet.http .*;
Import java.util .*;
Public class ShowRequestHeaders extends HttpServlet (
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException (
Response.setContentType (text / html);
PrintWriter out = response.getWriter ();
String title = Display all requests head;
Out.println (ServletUtilities.headWithTitle (title) +
<BODY BGCOLOR=\#FDF5E6\> \ n +
<H1 ALIGN=CENTER> title + + </ H1> \ n +
<B> Request Method: </ B> +
Request.getMethod () + <BR> \ n +
<B> Request URI: </ B> +
Request.getRequestURI () + <BR> \ n +
<B> Request Protocol: </ B> +
Request.getProtocol () + <BR> <BR> \ n +
<TABLE BORDER=1 ALIGN=CENTER> \ n +
<TR BGCOLOR=\#FFAD00\> \ n +
<TH> Header Name <TH> Header Value);
Enumeration headerNames = request.getHeaderNames ();
While (headerNames.hasMoreElements ()) (
String headerName = (String) headerNames.nextElement ();
Out.println (<TR> <TD> + headerName);
Out.println (<TD> + request.getHeader (headerName));
)
Out.println (</ TABLE> \ n </ BODY> </ HTML ");
)
Public void doPost (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException (
DoGet (request, response);
)
)
↑ Back






