File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Is setContentType() optional? 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 "Is setContentType() optional?" Watch "Is setContentType() optional?" New topic

Is setContentType() optional?

James Kwan
Ranch Hand

Joined: Dec 06, 2001
Posts: 51
Does anyone know if setContentType() needs to always be called to set the content type for the response prior to calling getWriter(). If not, what is the default setting or will there be an exception thrown?
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I am not too sure abt servlets, but in JSPs the Specs say that it is optional. The default for JSPs is "text/html".
Will update on servlets as I find out.....
- satya

Take a Minute, Donate an Hour, Change a Life
Meng Tan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
I think it may be server dependent.
Srini Admala
Ranch Hand

Joined: Dec 04, 2001
Posts: 50
While using servlets, setContentType() has to be explicitly done before getting the PrintWriter.
////////// code ///////////////////////
PrintWriter out = response.getWriter();
And in Jsp's contentType is by default "text/html" for page directive.

Hope this helps.
Stephen Tallamy

Joined: Jan 08, 2002
Posts: 16
I'm not sure if setContentType() HAS to be called, although it is preferrable. I've developed a Servlet without it and it runs in Tomcat fine. It does produce some rather odd results though, as sometimes it the font of the text changes.
I agree. Here's the link:
subject: Is setContentType() optional?
It's not a secret anymore!