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

question from HFSJ

 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is it:

1). When using a RequestDispatcher, the use of which methods can often lead to an IllegalStateException?

(a) read
(b) flush
(c) write
(d) getOutputStream
(e) getResourceAsStream

the answer in page 215 was (b), but then i check the errata, it said :

[215] Answer to question 1;
Option C should also be checked.

-----
write method also cause that above exception???

can i trust on the errata???
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the errata is wrong. Because if you just do a write and then forward, the container will just clear the response buffer and then forward to the resource so that the forwarded resource can start with a fresh response buffer. So whatever you write would be gone if you do a forward. Only when you flush, you commit the response and an IllegalStateException would be caused. This will become more clear in one of the JSP chapters, where the book explains what will happen when you write to the response and then use jsp:forward (which basically does a RequestDispatcher forward)
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think option C is also correct. The question is a tricky one. If we observe the wording "the use of which methods can often lead to an IllegalStateException" - that means it MAY lead, not MUST lead. When we call the write() method, if the buffer gets full, it is flushed and the response gets committed. The write() method MAY lead to IllegalStateException sometimes.

- Gouri
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes you can trust the errata.
Why? well the write method will send bytes to the OutputStream or the PrintWriter. These will store the bytes in a buffer before they are flushed to the client. But think about what happens if there is no buffer, they get sent straight to the client, and at that stage the response is committed, thus an IllegalStateException is thrown.

Mat
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i see. thanks guys....

hm...could you guys give an example of code to show the use of write method that MAY lead to IllegalStateException...?
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this :

response.setBufferSize(0);
PrintWriter out=response.getWriter();
out.write("Some Stuff");
request.getRequestDispatcher("/abc.jsp").forward(req,resp);

See whether you get the IllegalStateException
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tested the example from B. Satish with the current Tomcat-Version.

This doesn't works:

response.setBufferSize(0);
PrintWriter out = response.getWriter();
out.write("Some stuff");
out.flush();

RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);

But this works:

response.setBufferSize(0);
PrintWriter out = response.getWriter();
out.write("Some stuff");

RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);

response.setBufferSize(0) and write() do not lead to a IllegalStateException in my servlet. Thus, I'm still confused with the answer (c) in the mock-exam-question above.

Could anyone please help me to understand option (c) in the mock-exam-question?

Oliver
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question states "which method may lead to ISE".
The forward mechanism is defined as :
Forwarding servlet does not output anything to the client, but may do some processing, and even set headers.

With this in mind, using a write method on an outputstream (pw or sos) is a bad idea before a forward. The case where an exception is thrown is when the output written with the write method IS commited (ie sent to client) with the flush() method. If you don't flush buffer then clear buffer and forward, then the output you attempted to send with the write method may not bother you, so no exception occurs. If output has been committed/flushed (ie committed flag set to true) then the exception occurs.

So, as a conclusion, if you use write() method before a forward() AND you commit response, you surely get an Exception. Which means that the write method used with a RD MAY lead to an Exception.

Hence, answer C is correct.

EDIT : The goal of this question is obviously to make sure you understood that a forwarding servlet should NOT write anything to the response, as its goal is only a pre-processing role. Burn this in your mind, and SCWCD exam questions about this will be obvious.
[ March 20, 2006: Message edited by: Frederic Esnault ]
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fortunately the real exam tell how many correct answers are there...so we dont have to bother about this kind of difficulty...am i right???
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the real exam does give you the number of correct answers. But if it asks you to choose two and you know only one then what will you do? You'll have to pick one and hope you're lucky...

Or you know how things work and answers appear much more clear
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you are NOT sure with the correct answer you can easily wipe off the wrong ones as they are very apparent. And don't worry you will get questions like this one in real exam.
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frederic, thanks a lot for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic