• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flushing after Forward.

 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Servlet 'CallingServlet' uses the RequestDispatcher mechanism to forward
the control to another Servlet called 'CalledServlet'. What will be the output of the
following program?

a. The output will be 'Output from Calling Servlet' followed by 'Output from Called
Servlet'.
b. The output will be 'Output from Called Servlet' followed by 'Output from Calling
Servlet'.
c. The program will throw IllegalStateException at run-time.
d. The program will output 'Output from Called Servlet'.
e. None of the above.


Answer: D

In my opinion even C should have been there since CalledServlet commits response and when it comes back to CallingServlet it should throw IllegalStateException since it again tries to commit it.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the RequestDispatcher forward the request and response to other component of web app (servlet, JSPs , etc.) , it will not back to the CallingServlet. Unless the method you use is include() , however this method will normally be used in JSP either by <jsp:include ... > or <c:include>

However, there are still other Container which will return to the CallingServlet after the CalledServlet complete its job but this is not the correct implementation.

Therefore, Answer C will not be the correct answer

Remark: IllegalStateException will be thrown if Flush before Forward , because the response already committed/flush and it ends there.
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think answer is A , ignoring the fact some containers "will return to the CallingServlet after the CalledServlet complete its job but this is not the correct implementation. "

Please clarify.


Archana
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Kian Giap wrote:After the RequestDispatcher forward the request and response to other component of web app (servlet, JSPs , etc.) , it will not back to the CallingServlet. Unless the method you use is include()


I think tht stmt is not correct. In all FORWARD cases, control DOES return to CallingServlet after CalledServlet completes its job. That's inevitable.


Archana Annamaneni wrote:I think answer is A , ignoring the fact some containers "will return to the CallingServlet after the CalledServlet complete its job but this is not the correct implementation. "


Option A can't be the answer because in case of FORWARD buffer is cleared first, so alsthough line 5 does write "Output from Calling Servlet" to output buffer but it is cleared before FORWARD, so now CalledServlet receives a clean buffer response object so because of line 18 option C is correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic