• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpServletResponse doubt

 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
PrintWriter out = response.getWriter();
out.flush();
response.setContentType("myname");
out.println ("Worked....");
}

Should this method print "Worked...." on the browser ?

I think this should give an IllegalStateException, since out.flush() has already sent the response to the client and then we are trying to set the content type.

But this code works !

Anybody please explain.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Tomcat to test this? Which version of Tomcat is that? It should actually have thrown IllegalStateException.
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using WebSphere. Would this be a vendor specific problem ? I dont think so.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us why you thought that it would throw an IllegalStateException.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are setting the contentType after you flush the o/p. By default, before the flush, the contentType would be text/html and after flushing, you attempt to change it which according to me should produce error. Please correct me if I'm wrong.
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
PrintWriter out = response.getWriter();
out.flush(); //1
response.setContentType("myname");
out.println ("Worked....");
}
After stmt 1 is executed, the response has been sent to the client.
So after the response has been sent, how can we set the "content-type" for it ?
Hence I think we should get an IllegalStateException.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taken from setContentType API :

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. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is something new for me ! thanks !
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

but the code still must throw an exception at the "out.println ("Worked....");" because the "out.flush();" commits the response.
I am wrong ?

Regards
M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic