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 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

janne s
Ranch Hand

Joined: Sep 21, 2004
Posts: 81
Hi!

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");


Ans: throws:IILEGALSTATE EXCEPTION.

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
HI!

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
hi!

where does standard actions endup in jsp translated servlet?



thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher