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 doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher doubts" Watch "RequestDispatcher doubts" New topic
Author

RequestDispatcher doubts

Surajsingh Thakur
Ranch Hand

Joined: Sep 09, 2008
Posts: 124

Please can people help me with the way RequestDispatcher works in detail...
what will be the output of the following--->

1: out.println("Hello include");
request.getRequstDispatcher("myjsp.jsp").include(request,response);
out.println("include continued");
i want to know what happens to the code before and after we call include on requestdispatcher.............


2: out.println("Hello forward");
request.getRequstDispatcher("myjsp.jsp").forward(request,response);
out.println("forward continued");
i want to know what happens to the code before and after we call forward on requestdispatcher.............

Thank you all


BE (Computer Science) SCJP 4 (80%) SCWCD 5 (97%) IBM CAD(98%) IBM CAppD(100%) IBM SD(100%)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi suraj,

1.if you use servlet, i think here your using servlet
--------------------------------------------
out.println("Hello include");
request.getRequstDispatcher("myjsp.jsp").include(request,response);
out.println("include continued");
-----------------------------------------

suppose in myJSP.jsp:
suraj is good boy ,then

output is:

Hello include
suraj is good boy
include continued


2.in 2nd case you can see only suraj is a good boy
because your controll goes to the myJSP.jsp

but i recommended you to test case 1 in jsp

that is,
<body>
Hello include
request.getRequstDispatcher("myjsp.jsp").include(request,response);
include continued
</body>


now you will get differet answare
Sarat Koduri
Ranch Hand

Joined: Jul 08, 2008
Posts: 83
seetharaman ..

now you will get differet answare


sorry i did not think we do get a different output. do we.. ? please explain.
Rufus Addis
Ranch Hand

Joined: Aug 21, 2008
Posts: 83
When you do a forward the out buffer is cleared.

i.e. "Hello forward" gets written to buffer, buffer is cleared and control passed to JSP. JSP then is evaluated, and at end of JSP the buffer is flushed and closed. When control passes back to the servlet, the browser has rendered it's content so anything extra written is ignored.

So as seetharaman said you'll only get "suraj is good boy" in the browser in the second example.

HTH - Rufus.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi ,

sarat was asking about
in JSP
---------------------------------------
Hello include
<%request.getRequstDispatcher("myjsp.jsp").include(request,response); %>
include continued
-----------------------------------------

please past this code into your jsp and check it
Sarat Koduri
Ranch Hand

Joined: Jul 08, 2008
Posts: 83
.. unfortunate.. system got crashed with all handsome viruses..

so pelase tell me about the different output ..you are expecting me to see..
Rufus Addis
Ranch Hand

Joined: Aug 21, 2008
Posts: 83
Aha - guess who didn't read the answer properly!

Using RequestDispatcher in a JSP will have different results as the compiled JSP will use a JSPWriter which doesn't get sent to the output until releasePageContext() is called at the end of the JSP. So in theory the buffer will be empty when the dispatcher forwards!

i.e.

include.jsp writes "Hello include" to JSPWriter1
.include(r,r) is called and out is flushed
myjsp writes "suraj is a good boy" to JSPWriter2
releasePageContext is called for myjsp and JSPWriter2 is written to out.
include.jsp writes "include continured" to JSPWriter1
releasePageContext is called for include.jsp and JSPWriter1 is written to out.

So would expect the Hello include etc. to appear after suraj is a good boy!

Rufus.
Sarat Koduri
Ranch Hand

Joined: Jul 08, 2008
Posts: 83
Thank you Rufus, Thats a nice explanation...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Rufus Addis:

include.jsp writes "Hello include" to JSPWriter1
.include(r,r) is called and out is flushed
myjsp writes "suraj is a good boy" to JSPWriter2
releasePageContext is called for myjsp and JSPWriter2 is written to out.
include.jsp writes "include continured" to JSPWriter1
releasePageContext is called for include.jsp and JSPWriter1 is written to out.



Good Explanation buddy Rufus Addis .

but i doubt on BOLD LETTERs ... in case of out flush...then you should get IllegalStateOfException right?

please correct me,if i am wrong
Rufus Addis
Ranch Hand

Joined: Aug 21, 2008
Posts: 83
You're right, I meant cleared not flushed - MUST DRINK MORE COFFEE!

Rufus.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher doubts
 
Similar Threads
Illegatstateexception !!!
include of RequestDispatcher
Request dispatcher
RequestDispatcher
forward happens before service() finishes