File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setContentType and getOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "setContentType and getOutputStream" Watch "setContentType and getOutputStream" New topic
Author

setContentType and getOutputStream

Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
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


Kyle Tang<br />SCJP 91<br />SCWCD 96<br />SCBCD 95
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Yes I believe.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Raj Paul
Ranch Hand

Joined: Jul 09, 2002
Posts: 77
No, getOutputStream is used to write binary data. so i hope there will not be the need to call setContentType() method.
Raj Paul
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
Marcelo Castro
Greenhorn

Joined: Dec 19, 2001
Posts: 6
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

Joined: Aug 22, 2002
Posts: 78
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
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setContentType and getOutputStream