Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpServletReponse setContentType() method

 
Tyronne Fernando
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HttpServletResponse do we have to set Content Type by calling setContentType() method all the time? If we don't does it get set to "text/html" by default? I checked the Jave EE API and I couldn't find anything.

Any ideas? Thanks in advance.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP
The contentType attribute of the page directive defines the MIME type and the character encoding for the response of the JSP page.Values are either of the form "TYPE" or "TYPE;charset=CHARSET".The default value for "TYPE" is "text/html" for JSP pages in standard syntax, or "text/xml" for JSP documents in XML syntax.

Servlet: In case you don;t set content type on the ServletResponse object it is null.
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak ,

Sorry for re-initiating this thread but i dint understand below as till now i knew by default the Content-type if not set is text/html in case of servlets .


Servlet: In case you don;t set content type on the ServletResponse object it is null.


Could you please elaborate more?

Thanks
Vishal Chugh
 
Abraham Moyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting from the java 1.4 specification....
The charset for the MIME body response can be specified explicitly using the setCharacterEncoding(java.lang.String) and setContentType(java.lang.String) methods, or implicitly using the setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit specifications. If no charset is specified, ISO-8859-1 will be used. The setCharacterEncoding, setContentType, or setLocale method must be called before getWriter and before committing the response for the character encoding to be used.



I do not think the default null setting will be true, but I stand to be corrected.
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abraham Moyo,

Thanks for response .

If no charset is specified, ISO-8859-1 will be used.
If no content-type is specified then ? ( will deafult be text/html ? )

My deafult content-type doubt stil there , sorry.

Thanks
Vishal Chugh
 
Abraham Moyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal, always a pleasure.

Going deeper into it and from what I understand, if no content-type is set, it will be up to the container as to what the default value will be.
What is guaranteed is that the container will call the getContentType() method of the ServletResponse or HttpServletResponse

Quoting from the specification of that method, the method
Returns the content type used for the MIME body sent in this response.
...Whatever value would have been set, which in this case we have not. The method specification goes onto state that,
If no content type has been specified, this method returns null
....

And from my understanding it is now up to the container how to handle the returned null value.

Yt again, this is from deduction and I stand to be corrected.




[ December 09, 2008: Message edited by: Abraham Moyo ]
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for explaining !!!

Okay so its container's choice , for what can be used as default (null or text/html) for content-type if it has not been set by us explicitly .

Thanks
Vishal Chugh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic