• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setContentType Vs setHeader

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. I believe that I am clear with the doubt which i had.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic