• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is setContentType Optional?

 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for response.
I want add somthing that "text/html" is default contentType for every browser.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic