• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher doubts

 
Ranch Hand
Posts: 124
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
seetharaman ..

now you will get differet answare



sorry i did not think we do get a different output. do we.. ? please explain.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.. 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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rufus, Thats a nice explanation...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I meant cleared not flushed - MUST DRINK MORE COFFEE!

Rufus.
reply
    Bookmark Topic Watch Topic
  • New Topic