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

Head First JSP - Clarification on when a response is committed?

 
Alpha Ng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.
I am currently reading through the Head First JSP and Servlets book, but have encountered a point of confusion regarding Servlet response.

According to the book's errata, on page 215, the answer to number 1 should also "include option C". This means that when the out object is USED (not flushed), then the response is committed.

If it's when the output stream is USED, wouldn't this affect using the object in a JSP file?

Can someone provide some clarity on this? The book (and general consensus) seem to say different things at different parts.

Does the object get committed ....
1. when the out object (response.getWriter, or the JspWriter object) is used?
2. when the out object is flushed?
3. Both of the above? (ref. Question #1 of Chapter 5 mock exam)
4. when the service method of the servlet (_jspService of JSP) is returned?

Thanks.
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to the spec, the response is committed when the out.flush() is called because that is when it is guaranteed that the response written to the buffer has been sent to the client
 
Akshay Singhvi
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,
Adding to what Alpha has asked, can we use both write and println method with same response object.What I mean is :
Can we do this with the same response object
response.write();
response.println("");
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no......
you cant do both on a response object
i have read this in some of the chapter mocks in david bridgewater / hanumant deshmukh books but i dont rememebr exactly where
 
Boobalan Pillai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Akshay Singhvi :

I think you wont get both the write() and println() methods from the same objects.

os=resp.getOutputStream();
os.write(5);

out = resp.getWriter();
out.println("</title></head><body>");

if you use both the OutputStream and writer object you will get java.lang.IllegalStateException

Regards
Bala
preparing (SCWCD)
 
Alpha Ng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone comment on why the book's errata have that answer?
 
Alpha Ng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
polite bump :-/
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer of this puzzle is hidden in following servlet specs:

A servlet container is allowed, but not required, to buffer output going to the client for efficiency purposes.


and

When using a buffer, the container must immediately flush the contents of a filled buffer to the client.


and mind the language of question :
When using a RequestDispatcher, the use of which methods can often lead to an IllegalStateException?


So if write will cause buffer to be filled. The buffer will be flushed and it can cause requestDispatcher to throw IllegalStateException.

Please correct me if I am wrong?
[ November 15, 2006: Message edited by: Arvind Giri ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic