HTTP Response Headers methods

Hi,

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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
What exactly is the question?
0
gudii9Author Commented:
When to use which header method and how they are different from servlet 2.1 vs servler 2.2. Please advise
0
girionisCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.