aspose file tools*
The moose likes Servlets and the fly likes setContentType Vs setHeader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setContentType Vs setHeader" Watch "setContentType Vs setHeader" New topic
Author

setContentType Vs setHeader

Abdul Mujeeb Md
Greenhorn

Joined: Aug 25, 2008
Posts: 26
Hi,

I knew using setHeader we can achieve same funcionality as setContentType.

Eg:


response.setContentType("text/html");

is same as


response.setHeader("content-type","text/html");



The doubt I got is while downloading an attachment in a particular format.

If i use
response.setHeader("Content-disposition","attachment;filename=myFilePrint.xls");



The file is getting opened in an excel application. If this is so, then what is the use of content type? When the above setHeader automatically detects the extension of the file name given, if the extension is not provided it opens choose program window defined in my Operating System.

Could you clear me on this issue?

Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly there are differences between browsers and how they detect file types using the content type and file name. Therefore providing both gives you the best chance that the response will behave as you expect. Omitting on or the other may have a different effect again.

Also, providing a file name is not the preferred mechanism for determining the content type (remember we're talking content type, not necessarily file type) even though IE may tell you otherwise.
Abdul Mujeeb Md
Greenhorn

Joined: Aug 25, 2008
Posts: 26
Thanks for your reply. I believe that I am clear with the doubt which i had.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Content type is for the browser to prepare itself to deal with the content it is about to receive. If you check the headers for a request sent by the browser, you can say that content-type of response corresponds to Accept header of request. Thus, there are default formats with which the browsers can deal without much effort from your side. However, for others, you need to take appropriate care.


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setContentType Vs setHeader