Hi All Please help me in solving the below mentioned Question:
Q: Consider the following code snippets. What will be displayed on the browser when a GET request is sent to FirstServlet assuming that the buffer is large enough to hold all the data before sending the data to the client? Code Snippet:
As per my understanding Option 1 should be correct as I have tested this case.
Code Snippet =================== 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");
After any Requestdispatch or sendRedirect or jsp:forward, all the contents of the forwarding page are lost. That's what is happening for the Page1. Also, after the RequestRedirect, the control from page1 is transferred to the called jsp, so "page3" never gets printed. The reason why the IllegalStateexception won't occur, is that, the response is never "committed" or "sent to the browser". Hope it helps.