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

Content-type of HttpServletReponse

 
Maciek Mike
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I need to call setContentType() every time before I'm about to obtain getWriter() object from HttpServletResponse.

It should be done this way:



Is also correct to do it like that:


 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you specify a content type of image/jpeg and then write text as output?? The content type is used to help the browser understand what type of content you are sending to it. If you don't set the content type, you might get unexpected behaviors in the browser...
 
Maciek Mike
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right. "image/jpeg" is not the best choice here :-)

I posted this question since I found similar one (also with "image/jpeg") in my preparation kit.
They stated that I must call setContentType before calling getWriter() method.
I assume that is not true. Am I right?
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should call the setContentType method before writing any response, because if you don't, then the browser might not be able to understand what type of content you are sending. But you are not forced to set the content type...
 
Maciek Mike
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic