Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Effect of Calling setContextType() more than once

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What will be the effect?

1. Some Exception?
2. Statement A) will be effective
3. Statement B) will be effective

I know it's not practically correct but what will be the output in this case ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's quite easy to test, isn't it?
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statement B will be effective.

According to servlet specification:
This method may be called repeatedly to change content type and character
encoding. This method has no effect if called after the response has been
committed.


As nothing has been written to the output stream and flush method has not been called, so the response is not commited.
[ March 24, 2007: Message edited by: Tridib Samanta ]
 
arun shanmugam kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Statement B will not have any effect according to page 46 and 47 of the servlet spec 2.4.

If setContentType method is not called before calling getWriter or if the response is committed then default ISO-8859-1 is used.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If setContentType method is not called before calling getWriter or if the response is committed then default ISO-8859-1 is used.


It is only the character encoding that will take no effect, but the content type will be changed (unless you commited the response before).

I've just tried:

And the result I get is a "text/html" page that uses the UTF-8 encoding.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!