• 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

Q. on setContentType()

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is mentioned in the API doc that

"If obtaining a PrintWriter, this method should be called first."

I have a code which does otherwise but doing just fine. What is the implication if the getPrintWriter() is called first before setContentType()?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the implication if the getPrintWriter() is called first before setContentType()?



It would use the default content type which is "text/html."
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sumit Sadana:


It would use the default content type which is "text/html."

Cheers!~
Sumit


Hi,

It does not look like that way. When I modified the program to set the response to "application/jar", the browser treated the output as a download, which is what is expected for that content type:

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
API specs give you the correct way of doing things.

But Containers handler some of the things on their own. Every container does some of its own stuff. So, API is right that we should do it that way but container handling makes it failsafe.

But the big question is...if we get something like this in the exam then what should we write.

I feel that we should go the way it is there in API specifications. Because the exam is prepared by Sun and API specs are theirs also.

I hope it helps!
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kapil munjal:
API specs give you the correct way of doing things.



The API specs only tells you what you should do. However,it does not give any explanation on why you should do it this way. That is why I asked what is the implication if you get the printwriter first before you set the content type. If it does not give out any error then what is the problem. Could it be that the behaviour is non-deterministic?
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats right..API doesn't give you this Idea "why this approach".

The API always describe things in a very easy and appropriate manner.

If in this example, when we are setting the contentType first and then creating an object of printwrite, it means that whatever we are going to write has been instructed to the container by this command. And then creating the object for writing the content in the response.

response.setContentType("text/html");
PrintWrite out = response.getWriter();

But if we create the object for writing to the response and set the contentType to app/jar. Then writer object is and overhead for the servlet.

Well, there is no way, it is going to give some error but definitely this approach doesn't look right.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic