• 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 and getOutputStream

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a question about setContentType() and getOutputStream(), getWriter().
setContentType() should be called before calling getWriter(), as is said on the API spec. Should we call setContentType() before calling getOutputStream() too?
Thanks!
Kyle
 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I believe.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, getOutputStream is used to write binary data. so i hope there will not be the need to call setContentType() method.
Raj Paul
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is always a need to set the content type, especially when writing binary data. After all, if you just present the browser with a splotch of binary gloop, how is it supposed to know whether it's a JPEG image (image/jpeg), GIF image (image/gif), PNG image (image/png), AVI movie, QuickTime movie, MIDI file, Word document, or some format it doesn't know and needs to pop up a "Save As..." dialog box for?
- Peter
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter is right. Givin� an additional information: if you don�t specify the method setContentType, the type "text/html" will be evaluated automatically
keep studying
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
My understanding now is,
1. if need to call setContentType() and getWriter(), then always call setContentType() first. The reason is setContentType() may change the charset, which affect the PrintWriter.
2. setContentType() doesn not need to be called before calling getOutputStream()
3. if no setContentType() called, content type is "text/html;charset=iso-8859-1".
Kyle
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the summary Kyle. I believe I may have misinterpreted RP's reply -- rereading this thread, it is clear that he meant there is no need to call setContentType before calling getOutputStream(). Which is correct (although you should call it before writing any great quantities of data to this stream; in fact it is good style to set the content type before writing any data to the output stream).
- Peter
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic