• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is setContentType() optional?  RSS feed

 
Ranch Hand
Posts: 51
  • 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?
 
Ranch Hand
Posts: 5040
  • 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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it may be server dependent.
 
Ranch Hand
Posts: 50
  • 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
 
Greenhorn
Posts: 16
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!