Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference bwtween jsp:forward and RequestDispatcher forward.

 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiang Wang
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gabriel Forro
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shiang Wang
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabriel was right, I spoke from my memory, got confused with request.sendRedirect().
 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic