File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on setContentType() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt on setContentType() method" Watch "Doubt on setContentType() method" New topic
Author

Doubt on setContentType() method

prashant k. gupta
Ranch Hand

Joined: May 15, 2008
Posts: 62
Please clarify me about response.setContentType(), whether its required to set content type before writing in to response... or is there default method for content type if we don’t explicitly define setContentType().

In below example if we don’t set content type, as its commented for contentType , will it throw the runtime exception or it will work fine. How the content will be shown in browser.


please explain. Thanks In advance.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


It is always recommended to set ContentType() to the response that you generate. It tells the browser on how to handle the incoming response data.

however if you don't set it , it will still run considering as text/html
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi Prashant,

No it is not mandatory required if you are not set it will take default value text/html

SetContentType() is used to intimate the browser that which type of content is coming as response,Depending upon the contentType browser treats with the response.

  • ContentType is also called as MIME(MultipurposeInternetMailExtension)type


  • Using this type we can set the response to the client or browser or useragent


  • it will give the response based upon the ContentType


  • default MIME type is(”text/html”)





  • Thanks, Nishan Patel
    SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
    prashant k. gupta
    Ranch Hand

    Joined: May 15, 2008
    Posts: 62
    Thanks a lot Nishan/Balu for your response.Now i am clear.
    Harshana Dias
    Ranch Hand

    Joined: Jun 11, 2007
    Posts: 327
    will it throw the runtime exception or it will work fine. How the content will be shown in browser.


    it would be better if you try it by your self and see what will hapen then ask what is the reason for that behaviour you saw
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt on setContentType() method
     
    Similar Threads
    setting characters encoding
    HttpServletResponse& HttpServletRequest
    is setContentType Optional?
    Problem with JSPs in web application
    Session tracking with URL rewriting