Tatarao voleti
Tatarao voleti

Reputation: 513

What is the use of response.addHeader when we have response.setContentType in java

May i know the use of response.addHeader when we already have response.setContentType in java... I m unable find proper solution.

<% response.addHeader("Content-Disposition","attachment;filename=Report.xls"); %>

<% response.setContentType("application/vnd.ms-excel"); %>

Here the above second statement is enough to get response as excel format. which scenario i need to use response.addHeader ?

please ...

Upvotes: 4

Views: 4884

Answers (2)

AllTooSir
AllTooSir

Reputation: 49362

This particular header :

"Content-Disposition","attachment;filename=Report.xls"

tells the browser to download the file as an attachment with default name Report.xls.

Also check the HTTP/1.1 specs

The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file.

An example is

    Content-Disposition: attachment; filename="fname.ext"

The receiving user agent SHOULD NOT respect any directory path information present in the filename-parm parameter, which is the only parameter believed to apply to HTTP implementations at this time. The filename SHOULD be treated as a terminal component only.

If this header is used in a response with the application/octet- stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.

Remember though , HTTP/1.1 defines the Content-Disposition response header field, but points out that it is not part of the HTTP/1.1 Standard.


IMHO , don't use JSP to do the downloading stuff , use a Servlet instead !

Upvotes: 6

vilicvane
vilicvane

Reputation: 12413

I don't write in Java, but I know there are many sharing the same feature.

And IMO it's just a shortcut.

BTW, there are so many headers... Not just Content-Type or something like that.

Upvotes: 0

Related Questions