*When the HTTP client (e.g. a browser) sends a request, it is required to supply a request line (usually GET or POST).
* If it wants to, it can also send the number of headers, all of which are optional except for Content-Length, which is required only for POST requests
HTTP Request Header Methods Available in Servlets
General:
-getHeader (header name is not case sensitive)
-getHeaderNames
-getHeaders
Specialized
-getCookies
-getAuthType and getRemoteUser
-getContentLength
-getContentType
-getMethod
-getRequestURI
-getQueryString
-getDateHeader
-getIntHeader
-Related info
-getProtocol
An Overview of Request Headers
When the HTTP client (e.g. a browser) sends a request, it is required to supply a request line (usually GET or POST). If it wants to, it can also send the number of headers, all of which are optional except for Content-Length, which is required only for POST requests. Here are the most common headers:
* Accept The MIME types that the browser prefers.
* Accept-Charset The character set that the browser expects.
*
Accept-Encoding The types of data encodings (such as gzip) that the browser knows how to decode. Servlet can explicitly check for gzip support and return gzipped HTML pages to browsers that support them, setting the Content-Encoding response header to indicate that they are gzipped. In many cases, this can reduce page download times by a factor of five or ten.
*
Accept-Language The language the browser is expecting, in case the server has versions in more than the one language.
*
Authorization Authorization info, usually in response to the WWW-Authenticate header from the server.
*
Connection Use persistent connection? If a servlet get a Keep-Alive value here, or gets a request line indicating HTTP 1.1 (where persistent connections are the default), it may be able to take advantage of persistent connections, saving significant time for Web pages that include several small pieces (images or applet classes). To do this, it needs to send the Content-Length header in the response, which is most easily accomplished by writing into a ByteArrayOutputStream, then looking up the size just before writing it out.
*
Content-Length (for POST messages, how much the data is attached)
*
Cookie (one of most important headers; see separate section in this tutorial on handling cookies)
*
From (email address of the requester; only used by Web spiders and other custom clients, not by browsers)
* Host (host and the port as listed in the original URL)
*
If-Modified-Since (only return documents newer than this, otherwise send the 304 "Not Modified" response)
*
Pragma (the no-cache value indicates that the server should return the fresh document, even if it is a proxy with a local copy)
*
Referer (the URL of the page containing the link the user followed to get to the current page)
*
User-Agent (type of the browser, useful if servlet is returning browser-specific content)
*
UA-Pixels, UA-Color, UA-OS, UA-CPU (nonstandard header sent by some Internet Explorer versions, indicating screen size, color depth, operating system, and cpu type used by the browser's system)
Reading Request Headers from Servlets
Reading headers is the very straightforward; just call the getHeader method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise. However, there are the couple of headers that are so commonly used that they have special access methods. The getCookies method returns contents of the Cookie header, parsed and stored in an array of Cookie objects. See the separate section of this tutorial on cookies. The getAuthType and getRemoteUser methods break Authorization header into its component pieces. The getDateHeader and getIntHeader methods read the specified header and then convert them to Date and int values, respectively.
Rather than looking up one of the particular header, you can use the getHeaderNames to get an Enumeration of all header names received on this particular request.
Finally, in addition to looking up request headers, you can get information on the main request line itself. The getMethod method returns the main request method (normally GET or POST, but things like HEAD, PUT, and DELETE are possible). The getRequestURI method returns URI (the part of the URL that came after the host and port, but before the form data). The getRequestProtocol returns third part of the request line, which is generally "HTTP/1.0" or "HTTP/1.1".
Popular Posts
-
INTRODUCTION TO 'C': C is a programming language developed at AT & T's Bell laboratories of USA in 1972.it was designed by d...
-
The SQL COUNT aggregate function is used to count the number of rows in a database table. The SQL COUNT syntax is simple and looks like this...
-
The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FR...
-
Files in VB .NET Working with Directories We will work with the File and Directory classes in this section. We will create a directory and c...
-
More Events The Form has more events besides the Form_Load event. How can you find them? Click on the Drop-Down List that found in the upper...
-
The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset with unique entries for certain database table col...
-
Steps to follow: 1. Write an abstract class called LivingThing.java as shown below in Code-11.4.a. (You are welcome to do this work using ei...
-
Introduction: If you are new to Exception handling, please read "Exception Handling Statements" section of the Java Progamming Tut...
-
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms: ...
-
Lab exercises and homeworks: * Things to check before you start the lab * Chapter 3 (Class #1, Jan. 16th homework) o Exerc...
No comments:
Post a Comment