What is response.addHeader when we have response.setContentType in java

Can I find out the use of response.addHeader when we already have response.setContentType in java ... I cannot find the correct solution.

<% response.addHeader("Content-Disposition","attachment;filename=Report.xls"); %> <% response.setContentType("application/vnd.ms-excel"); %> 

Here, the aforementioned second statement is enough to get the answer as an excel format. In what scenario do I need to use response.addHeader?

you are welcome...

+4
source share
2 answers

This specific header:

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

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

Also check HTTP / 1.1 specifications.

The Content-Disposition response header field was suggested as meaning that the source server will offer a default file name if the user requests to save the contents to a file.

An example is

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

The receiving user agent MUST NOT observe any directory path information is present in the filename-parm parameter, which is the only parameter that is believed to be applicable to HTTP implementations at present. filename MUST only be considered a terminal component.

If this header is used in a response with an application / octet-streaming content type, the implied assumption is that the user agent should not display the response, but directly enter the save response as ... ".

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


IMHO, do not use JSP to download files, use Servlet! Instead

+6
source

I do not write in Java, but I know that many use the same function.

And IMO is just a shortcut.

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

0
source

Source: https://habr.com/ru/post/1494224/


All Articles