HTTP Response Headers methods


I was reading as follows

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.

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.

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

Please advise.

Any links resources ideas highly appreciated. Thanks in advance
Who is Participating?
girionisConnect With a Mentor Commented:
It depends on what you want to do. For example if you want to use a cookie you should set the "Set-Cookie" header, or use the addCookie method which does this for you. If you want to display a PDF you set the content-type accordingly.

All these methods you see in the servlet API encapsulate the HTTP protocol, so instead of having to write the HTTP request by hand you use the servlet API.

If you want to delve deeper into this and understand what each header is and how they can be used then have a look at the HTTP protocol.
What exactly is the question?
gudii9Author Commented:
When to use which header method and how they are different from servlet 2.1 vs servler 2.2. Please advise
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.