Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
144
Chapter 7 Generating the Server Response: HTTP Response Headers
the header value. As with setting status codes, you must specify headers
before returning the actual document. With servlets version 2.1, this means
that you must set the headers before the first use of the 
PrintWriter
 or raw
OutputStream
 that transmits the document content. With servlets version
2.2 (the version in J2EE), the 
PrintWriter
 may use a buffer, so you can set
headers until the first time the buffer is flushed. See Section 6.1 (Specifying
Status Codes) for details.
Core Approach
Be sure to set response headers before sending any document content to 
the client.
In addition to the general purpose 
setHeader
 method, 
HttpServlet 
Response
 also has two specialized methods to set headers that contain dates
and integers:
setDateHeader(String header, long milliseconds)
This method saves you the trouble of translating a Java date in 
milliseconds since 1970 (as returned by 
System.currentTimeMillis
, 
Date.getTime
, or 
Calendar.getTimeInMillis
) into a GMT time string. 
setIntHeader(String header, int headerValue)
This method spares you the minor inconvenience of converting 
an 
int
 to a 
String
 before inserting it into a header.
HTTP allows multiple occurrences of the same header name, and you
sometimes want to add a new header rather than replace any existing header
with the same name. For example, it is quite common to have multiple
Accept
 and 
Set Cookie
 headers that specify different supported MIME
types and different cookies, respectively. With servlets version 2.1, 
set 
Header
, 
setDateHeader
 and 
setIntHeader
 always add new headers, so
there is no way to  unset  headers that were set earlier (e.g., by an inherited
method). With servlets version 2.2, 
setHeader
, 
setDateHeader
, and
setIntHeader
  replace any existing headers of the same name, whereas
addHeader
, 
addDateHeader
, and 
addIntHeader
 add a header regardless of
whether a header of that name already exists. If it matters to you whether a
specific header has already been set, use 
containsHeader
 to check. 
Finally, 
HttpServletResponse
 also supplies a number of convenience
methods for specifying common headers. These methods are summarized as
follows.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved