Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher

 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!

where does standard actions endup in jsp translated servlet?



thank you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic