JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Generating the Server Response: HTTP Response Headers

  • Format of the HTTP response

  • Setting response headers

  • Understanding what response headers are good for

  • Building Excel spread sheets

  • Generating JPEG images dynamically

  • Sending incremental updates to the browser


Settiing Response Header From Servlet

  •  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.

setContentType

    This method sets the Content-Type header and is used by the majority of servlets. 

setContentLength

This method sets the Content-Length header, which is useful if the browser supports persistent (keep-alive) HTTP connections.


addCookie

This method inserts a cookie into the Set-Cookie header. There is no corresponding setCookie method, since it is normal to have multiple Set-Cookie lines.


sendRedirect

the sendRedirect method sets the Location header as well as setting the status
code to 302.



HTTP 1.1  Response Header And Their Meaning

  • Allow
    • The Allow header specifies the request method(GET, POST) that the server supports. The default service method of servlet automatically generates the header for OPTIONS request.

  • Cache- control
    •  This useful header tells the browser or other client the circumstances in which the response document can safely be cached. It has following possible values.
      •  public
      •  private
      •  no-cache
      • no-store
      • must-revalidate
      • proxy-revalidate
      • max-age
  • Connection
    • A value of close for this response headers instruct the browser not to use persistent HTTP connections
  • Content-Disposition
    •  It lets you request that the browser ask the use to save the response to disk in a file of the given name. It is used as follows:Content-Disposition:attachment;filename-file-name
  • Content-Encoding
    • It indicates the way in which the page was encoded during transmission. The browser should reverse the encoding before deciding what to do with the document.
  • Content-length
    • It indicates the number of bytes in the response. This information is using only if the browser is using persistent HTTP connection.

  • Content- Type
    •  It gives the MIME(Multipurpose Internet Mail Extension) type of the response document.

  • Expires
    • The header stipulates the time at which the content should be considered out-of-date and thus no longer be cached. A servlet might use this header that changes relatively frequently.

  • Last- Modified
    •  This very useful header indicates when the document was last changed. The client can then cache the document and supply a date by If-Modified-Since request header in later request.
  • Location
    • This header, which should be included with all responses that have a status code in the 300s, notifies the browser of the document address.
  • Pragma  
    • Supplying this header with a value of no-cache instructs HTTP 1.0 clients not to cache the document.
  •  Refresh
    • This header indicates how soon the browser should ask for an updated page. For example, to tell the browser to ask for a new copy in 30 seconds, you would specify a value of 30 with

Response.setIntHeader(“Refresh”,30)

 

  •  Retry-After
    • This header can be used in conjunction with a 503 response to tell the client how soon it can repeat the request

  • WWW-Authenticate
    • This header is always included with 401 status code. It tells the browser what authorization type(BASIC or DIGEST) and realm the client should supply in its Authorization header.

 Example