File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is setContentType() optional?

 
James Kwan
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Meng Tan
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it may be server dependent.
 
Srini Admala
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While using servlets, setContentType() has to be explicitly done before getting the PrintWriter.
////////// code ///////////////////////
response.setContentType("text/html");
PrintWriter out = response.getWriter();
////////////////////////////////////////
And in Jsp's contentType is by default "text/html" for page directive.

Hope this helps.
-Srini
 
Stephen Tallamy
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic