Sei sulla pagina 1di 6

28/03/13

Servlet Tutorial: Reading HTTP Request Headers

Reading HTTP Request Headers


This tutorial is now out of date. Please see the following sites for up-to-date information with downloadable source code. Intermediate servlet and JSP tutorial Advanced servlet and JSP tutorial JSF 2.0 tutorial Apache Tomcat Tutorial -- setup and introduction (includes Eclipse integration) Ajax tutorial GWT tutorial (Google Web Toolkit) jQuery tutorial Java tutorial

1. An Overview of Request Headers 2. Reading Request Headers from Servlets

3. Example: Printing all Headers 4. Servlet and JSP Tutorial: Top 5. Servlet and JSP Training Courses On-site at your company or at public venues.

1. An Overview of Request Headers


When an HTTP client (e.g. a browser) sends a request, it is required to supply a request line (usually G E Tor P O S T ). If it wants to, it can also send a number of headers, all of which are optional except for C o n t e n t L e n g t h , which is required only for P O S Trequests. Here are the most common headers: A c c e p tThe MIME types the browser prefers. A c c e p t C h a r s e tThe character set the browser expects. A c c e p t E n c o d i n gThe types of data encodings (such as gzip) the browser knows how to decode. Servlets can explicitly check for gzip support and return gzipped HTML pages to browsers that support them, setting the C o n t e n t E n c o d i n gresponse header to indicate that they are gzipped. In many cases, this can reduce page download times by a factor of five or ten. A c c e p t L a n g u a g eThe language the browser is expecting, in case the server has versions in more than one language. A u t h o r i z a t i o nAuthorization info, usually in response to a W W W A u t h e n t i c a t eheader from the server. C o n n e c t i o nUse persistent connection? If a servlet gets a K e e p A l i v evalue 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 a C o n t e n t L e n g t hheader in the response, which is most easily accomplished by writing into a B y t e A r r a y O u t p u t S t r e a m , then looking up the size just before writing it out. C o n t e n t L e n g t h(for P O S Tmessages, how much data is attached) C o o k i e(one of the most important headers; see separate section in this tutorial on handling cookies)
www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html 1/6

28/03/13

Servlet Tutorial: Reading HTTP Request Headers

F r o m(email address of requester; only used by Web spiders and other custom clients, not by browsers) H o s t(host and port as listed in the original URL) I f M o d i f i e d S i n c e(only return documents newer than this, otherwise send a 304 "Not Modified"

response) P r a g m a(the n o c a c h evalue indicates that the server should return a fresh document, even if it is a proxy with a local copy) R e f e r e r(the URL of the page containing the link the user followed to get to current page) U s e r A g e n t(type of browser, useful if servlet is returning browser-specific content) U A P i x e l s ,U A C o l o r ,U A O S ,U A C P U(nonstandard headers sent by some Internet Explorer versions, indicating screen size, color depth, operating system, and cpu type used by the browser's system) For full details on HTTP headers, see the specifications at http://www.w3.org/Protocols/.

2. Reading Request Headers from Servlets


Reading headers is very straightforward; just call the g e t H e a d e rmethod of the H t t p S e r v l e t R e q u e s t , which returns a S t r i n gif the header was supplied on this request, n u l lotherwise. However, there are a couple of headers that are so commonly used that they have special access methods. The g e t C o o k i e smethod returns the contents of the C o o k i eheader, parsed and stored in an array of C o o k i eobjects. See the separate section of this tutorial on cookies. The g e t A u t h T y p eand g e t R e m o t e U s e rmethods break the A u t h o r i z a t i o nheader into its component pieces. The g e t D a t e H e a d e rand g e t I n t H e a d e rmethods read the specified header and then convert them to D a t eand i n tvalues, respectively. Rather than looking up one particular header, you can use the g e t H e a d e r N a m e sto get an E n u m e r a t i o nof all header names received on this particular request. Finally, in addition to looking up the request headers, you can get information on the main request line itself. The g e t M e t h o dmethod returns the main request method (normally G E Tor P O S T , but things like H E A D ,P U T , and D E L E T Eare possible). The g e t R e q u e s t U R Imethod returns the URI (the part of the URL that came after the host and port, but before the form data). The g e t R e q u e s t P r o t o c o lreturns the third part of the request line, which is generally "H T T P / 1 . 0 " or "H T T P / 1 . 1 ".

3. Example: Printing all Headers


Here's a servlet that simply creates a table of all the headers it receives, along with their associated values. It also prints out the three components of the main request line (method, URI, and protocol).

3.1 ShowRequestHeaders.java
You can also download the source or try it on-line.
p a c k a g eh a l l ; i m p o r tj a v a . i o . * ; i m p o r tj a v a x . s e r v l e t . * ; i m p o r tj a v a x . s e r v l e t . h t t p . * ; i m p o r tj a v a . u t i l . * ;
www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html 2/6

28/03/13

Servlet Tutorial: Reading HTTP Request Headers

p u b l i cc l a s sS h o w R e q u e s t H e a d e r se x t e n d sH t t p S e r v l e t{ p u b l i cv o i dd o G e t ( H t t p S e r v l e t R e q u e s tr e q u e s t , H t t p S e r v l e t R e s p o n s er e s p o n s e ) t h r o w sS e r v l e t E x c e p t i o n ,I O E x c e p t i o n{ r e s p o n s e . s e t C o n t e n t T y p e ( " t e x t / h t m l " ) ; P r i n t W r i t e ro u t=r e s p o n s e . g e t W r i t e r ( ) ; S t r i n gt i t l e=" S e r v l e tE x a m p l e :S h o w i n gR e q u e s tH e a d e r s " ; o u t . p r i n t l n ( S e r v l e t U t i l i t i e s . h e a d W i t h T i t l e ( t i t l e )+ " < B O D YB G C O L O R = \ " # F D F 5 E 6 \ " > \ n "+ " < H 1A L I G N = C E N T E R > "+t i t l e+" < / H 1 > \ n "+ " < B > R e q u e s tM e t h o d :< / B > "+ r e q u e s t . g e t M e t h o d ( )+" < B R > \ n "+ " < B > R e q u e s tU R I :< / B > "+ r e q u e s t . g e t R e q u e s t U R I ( )+" < B R > \ n "+ " < B > R e q u e s tP r o t o c o l :< / B > "+ r e q u e s t . g e t P r o t o c o l ( )+" < B R > < B R > \ n "+ " < T A B L EB O R D E R = 1A L I G N = C E N T E R > \ n "+ " < T RB G C O L O R = \ " # F F A D 0 0 \ " > \ n "+ " < T H > H e a d e rN a m e < T H > H e a d e rV a l u e " ) ; E n u m e r a t i o nh e a d e r N a m e s=r e q u e s t . g e t H e a d e r N a m e s ( ) ; w h i l e ( h e a d e r N a m e s . h a s M o r e E l e m e n t s ( ) ){ S t r i n gh e a d e r N a m e=( S t r i n g ) h e a d e r N a m e s . n e x t E l e m e n t ( ) ; o u t . p r i n t l n ( " < T R > < T D > "+h e a d e r N a m e ) ; o u t . p r i n t l n ( " < T D > "+r e q u e s t . g e t H e a d e r ( h e a d e r N a m e ) ) ; } o u t . p r i n t l n ( " < / T A B L E > \ n < / B O D Y > < / H T M L > " ) ; } p u b l i cv o i dd o P o s t ( H t t p S e r v l e t R e q u e s tr e q u e s t , H t t p S e r v l e t R e s p o n s er e s p o n s e ) t h r o w sS e r v l e t E x c e p t i o n ,I O E x c e p t i o n{ d o G e t ( r e q u e s t ,r e s p o n s e ) ; } }

3.2 ShowRequestHeaders Output


Here are the results of two typical requests, one from Netscape and one from Internet Explorer. You'll see the reason Netscape shows a C o o k i eheader when you get to the tutorial section on cookies.

www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

3/6

28/03/13

Servlet Tutorial: Reading HTTP Request Headers

www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

4/6

28/03/13

Servlet Tutorial: Reading HTTP Request Headers

Sitemap for coreservlets.com


Training
About the Instructor Course Reviews Intermediate Servlet & JSP Training Courses Advanced Servlet & JSP Training Courses JSF 2.0 Training Courses Ajax Training Courses Java Training Courses Customized On-Site Training Courses

Tutorials
Apache Tomcat Tutorial Intermediate Servlets & JSP Tutorial Advanced Servlets & JSP Tutorial Jakarta Struts Tutorial JSF 2 Tutorial Java Tutorial Ajax Tutorial jQuery Tutorial GWT Tutorial

Programming Resources
Specifications & Documentation Servers IDEs Java-Related Jobs JavaServer Faces (JSF) Books Web Hosting Providers Java Programming Resources

www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

5/6

28/03/13

Servlet Tutorial: Reading HTTP Request Headers

Public Training Course Schedule Books Core Servlets & JSP 2nd Ed. M ore Servlets & JSP Core Web Programming Core Servlets & JSP 1st Ed. Core Servlets & JSP E-Book M ore Servlets & JSP E-Book

Consulting
Our consulting services We are hiring!

Jobs

www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

6/6

Potrebbero piacerti anche