aspose file tools *
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference bwtween jsp:forward and RequestDispatcher forward. 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 "Difference bwtween jsp:forward and RequestDispatcher forward." Watch "Difference bwtween jsp:forward and RequestDispatcher forward." New topic
Author

Difference bwtween jsp:forward and RequestDispatcher forward.

Gaurav Sahni
Ranch Hand

Joined: Sep 03, 2003
Posts: 56
Yo CattlePeople,

i came across this some-where:
For <jsp:forward> ,
"If the page output is buffered, the buffer is CLEARED prior to forwarding.

If the page output is buffered and the buffer was flushed, an attempt to forward the request will result in an IllegalStateException. "

I tested this by creating a JSP, and found it CORRECT.





Here, the buffer was set to null. then i wrote some template text and then the forward. hence, the forward throws an exception.

Then instead of <jsp:forward>, i the used RequestDispatcher to forward the request. But this time, it worked surprisingly. i thought, doing a requestDispatcher forward and <jsp:forward> are one at the same thing. Are they different ? if yes, then in what sense ?


Thanx,
Gaurav.


To know recursion, you must first know recursion...
Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
Dispatcher is transparent, which means the URL in browser doesn't change.
While if you use jsp:forward, you will be forwarded to another url, you will that in the browser.

Shiang


SCBCD, SCWCD, SCJP
Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59
Hello,

I think, that Shiang is wrong (sorry Shiang). If you use <jsp:forward .../>, the url does not change in the browser on the client side.

The different behaviour of RequestDispatcher's forward function and <jsp:forward page="..."/> is (from the jsp spec - chapter JSP5.5):


or



If you use Tomcat, you likely get:
The buffer size is 8192

If you try:


you do not get any exception in Tomcat, as the real buffer size is 8192, and the PageContext's forward method has no information, that you set the buffer size to 0.

remark:
According to the servlet spec, the container can set the buffer size bigger, than it's specified in the page directive or by the HttpServletResponse's setBufferSize() method.
[ November 19, 2004: Message edited by: Gabriel Forro ]

Gabriel
Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
Gabriel was right, I spoke from my memory, got confused with request.sendRedirect().
Gaurav Sahni
Ranch Hand

Joined: Sep 03, 2003
Posts: 56
Thanx for providing the insight, Gabriel.
Based on this, i would say

"The buffer size alloted by the container to a page (8192 KB, for eg. in case of Tomcat) is the buffer for JspWriter object, which will write to output finally.

However,if in the page, one tries to set the buffer using,
response.setBufferSize(0);
then, it does not changes this pre-allocated buffer of the JspWriter.

(then, what is actually changed ? )

Sorry for the confusion, again.
Gaurav.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference bwtween jsp:forward and RequestDispatcher forward.