• Post Reply Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher include..  RSS feed

 
Antonio Giovanni
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boy, i am getting some unexpected results..
probably you guys can throw some light on this matter

In one of the JSP pages i have all the display with java scriptlets, JSP tags and all..

At the end i am looking forward to include the result from a Servlet(which actually includes a JSP at the end using RequestDispatcher).

So i put something like

at the end of the JSP (the first JSP , not the one included by the servlet)

what is really happening is, the result from this servlet is included in the middle of the JSP and not at the end. That is weird.

In the middle of a table, in one of the columns, i have the complete response of the servlet.

Infact it got split up like this

<TD wi
// the JSP included by the servlet
dth > something </TD> // Look the way word 'width' is broken ..!!!
<TD>

What is going wrong ??

Thanks alot for your ideas

PS: Is there any work around for this , or am i going wrong somewhere

 
Antonio Giovanni
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it solved ,.. but approached in another way

I used <jsp:include page="" />

But still curious to know why my first option worked weird.
I know there is some cause for that.

Thanks
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone asked a similar question on another forum.
Cutting and pasting response.

Scenario: A.jsp include B.jsp via jsp:include and C.jsp via requestDispatcher.
Observed result: C.jsp is printed first.

It has to do with when each cache is flushed.

Each of these JSP pages will build up its output text into a buffer. When that buffer is full, or you flush it manually, the text gets written to the underlying output stream.

The jsp:include uses the same buffer as its parent page. The output writer of the current page gets passed to it so that it can just append to the end.

When you short-circuit things by using the RequestDispatcher manually, you only pass along the request/response. The page invoked then obtains the output stream, and wraps its own buffer around it.

When a page is complete, it flushes its buffer to the output stream.
So the order is:
Page B completes and flushes its buffer. That result gets sent to A's buffer.
Page C completes and flushes its buffer. That result gets sent straight to the underlying response output stream.
Then page A finishes, flushes its buffer. All of its buffer (including what it got from B) now gets written to the outputstream.

Solutions:
You can manually flush the writer before you invoke the RequestDispatcher: ie out.flush(). If you try to forward/redirect after this though, it will raise an error.

If you really want to get complicated, create a ResponseWrapper, and when you invoke the RequestDispatcher, pass it a response object with an outputstream/writer that gets added to the current buffer (ie what it does with a jsp:include)

Or you can just use jsp:include.

here is some code to play with.
Try changing the flush attribute on the jsp:include to "true" and see what happens.
Uncomment the out.flush() command to get it to print in the "correct" order.




Cheers,
evnafets

[ February 22, 2006: Message edited by: Stefan Evans ]
[ February 22, 2006: Message edited by: Stefan Evans ]
 
Antonio Giovanni
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan,

That makes it very clear, what was going on

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!