Two Laptop Bag
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

setContentType Vs setHeader

Abdul Mujeeb Md

Joined: Aug 25, 2008
Posts: 26

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



is same as


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

If i use

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?

David O'Meara

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

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][/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
I agree. Here's the link:
subject: setContentType Vs setHeader
It's not a secret anymore!