Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is setContentType Optional?

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have some doubts about above code

1.In above code i have not set content type but still i am able to print
response.
2.What is difference between setContentType() and setHeader() methods.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Browsers have their own means to detect the content type
2. I think you've made a mistake in setContentType. It has only one parameter.

response.setContentType("text/html");
is the same as
response.setHeader("Content-Type","text/html");
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Content type: The charset for the MIME body response can be specified explicitly using the setContentType(String) method. Explicit specifications take precedence over implicit specifications. If no charset is specified, ISO-8859-1 will be used. The setContentType method MUST be called BEFORE getWriter and BEFORE committing the response for the character encoding to be used.

There are 2 ways to define content type:

ServletResponse.setContentType(String);
HttpServletResponse.setHeader("Content-Type", "text/html");

Reference
[ September 07, 2006: Message edited by: Saif Uddin ]
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for response.
I want add somthing that "text/html" is default contentType for every browser.
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot set the default content type for any browser . All that you can do is to set the content type for your response.

If you dont set it, the browser will use its own mechanism to detect the content

Its not compulsory to set the content type for your response
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic