This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic


janne s
Ranch Hand

Joined: Sep 21, 2004
Posts: 81

pls help me with this...
I've a doubt in this code..this from a jdiscuss mock exam

In the doGet() of FirstServlet:
PrintWriter out = response.getWriter();
out.println("<html><body>Page 1");
RequestDispatcher rd = request.getRequestDispatcher("NextServlet");
rd.forward(request, response);
out.println("<br>Page 3</body></html>");
In the doGet() of SecondServlet:
PrintWriter out = request.getWriter();
out.println("<br>Page 2");


when i exceuted it didn't throw any exception.

thank you
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
That is because of buffering. If the first print call would not be buffered, the code will throw IllegalStateException. That is what is assumed in the question.

About RequestDispatcher.forward():
1) Response must not be committed before call else IllegalStateException.
2) Any data written to response object after call is simply ignored.

BS<br />SCJP 1.4 88%<br />SCWCD 1.4 86%
janne s
Ranch Hand

Joined: Sep 21, 2004
Posts: 81

is this stmt correct:

RequestDispatcher.forward() is also a normal method call, so the control does return to the calling resource.
siva raju
Ranch Hand

Joined: Oct 10, 2003
Posts: 37
Yes, control returns to the calling resource like in any method. Only, you can't access the output stream.
If you insert <jsp:forward...> in a jsp, then in the translated servlet, the container might insert a 'return' statement immediately after the rd.forward() call.

Siva<br />-----<br />SCWCD 1.4, SCJP 1.4, SCBCD(Preparing), CCNA
janne s
Ranch Hand

Joined: Sep 21, 2004
Posts: 81

where does standard actions endup in jsp translated servlet?

thank you
I agree. Here's the link:
subject: RequestDispatcher
It's not a secret anymore!