File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
According to servlet spec 2.4:
The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client. If output data
exists in the response buffer that has not been committed, the content must be
cleared before the target servlet�s service method is called
.


What should happen if I execute the following code:

out.println(5);
dipatcher.forward(request, response);

where out is PrintWriter and dispatcher is RequestDispatcher.


Thanks, Tridib
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Niranjan Basnayake
Ranch Hand

Joined: Mar 25, 2006
Posts: 58
A request cannot be forwarded after the response has been sent to the client.There will be an IllegalStateException


SCJP,SCWCD
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

I believe that statement was meant for the makers of web containers, not us.

With your code, it is likely that the response will not be committed before the forward is called. In that case, the RequestDispatcher is responsible for clearing the writer's buffer before passing the request/response forward.

Only if you're dangerously curious, read on.

There's also a bit of a catch (for us, not the container makers) here. If the response is committed (meaning something got flushed), then you'd get an IllegalStateException. It's possible that a flush can occur even without explicitly calling flush()!!!

One possibility... PrintWriter (in servlets) can be created with an autoFlush value that flushes the output on every println statement. I doubt if any containers set this to true for PrintWriters (or, if they do, they might be returning a container-specific subclass of PrintWriter that treats that flag differently), but servlet spec doesn't say that they won't.

Also... JspWriter (in JSPs) can be created with an "autoFlush" flag that defaults to true (you can change it with the page directive). According to the JSP spec, the autoFlush here doesn't run on every println() call - instead it flushes only when the buffer's size has been exceeded (the buffer can also be changed with the page directive and the container maker is required to default it to no less than 8kb). If this were your scenario and your code had lots of previous println statements and they overfilled the buffer, the buffer would force a flush. But RequestDispatcher in a JSP breaks MVC principles anyway, so you really should never use it in a JSP.

Lesson learned: When using RequestDispatcher in the real world make sure it's the first thing run (filters are often a great place for them!). There's no good reason why it shouldn't be. The exam will probably only test you on knowing that the forward method following a flush method will cause an IllegalStateException, so you won't have to worry too much about this gray area of "I didn't call flush is it still ok?"

This response is based on my own research. If anyone sees that I have made a false statement, feel free to correct me.
[ February 28, 2007: Message edited by: Marc Peabody ]

A good workman is known by his tools.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

If anyone sees that I have made a false statement, feel free to correct me.

Nothing wrong, but to put it simply:
Servlets are the opposite of toilets. Never flush before leaving.


[My Blog]
All roads lead to JavaRanch
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Satou kurinosuke:
Servlets are the opposite of toilets. Never flush before leaving.

Thank you for not expanding this analogy to what happens in an autoFlush.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

oh, I missed that one
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
wow !

even i used to get boggled after seeing such code -

println();
forward();

good analogy -

Servlets are the opposite of toilets. Never flush before leaving.
;-)


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
Thanks Marc, thanks to everybody for your explanation. So, my understanding is that, if something is there in buffer, the RequestDispatcher will clear it before transfering control to the target. Right?
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
if something is there in buffer, the RequestDispatcher will clear it before transfering control to the target. Right?

Yes, if auto flush is true or buffer size is reached otherwise No
 
GeeCON Prague 2014
 
subject: RequestDispatcher