In the doGet() of FirstServlet: .... RequestDispatcher rd = response.getRequestDispatcher("SecondServlet"); rd.forward(request, response); In the doPost() of SecondServlet: PrintWriter out = response.getWriter(); out.println("<br>Page 2</body></html>");
According to the spec, the method called is based on the request. Inside the service method we have the function getMethod.
Based the request method type the doPost or doGet is called. In your example The firstservlet has doGet receiving a GET Method request. The First servlet forwards the request to the next servlet. so if the second servlet has doGet it will call that method else it will give exception..
This is because the same request and response objects of the first servlet is passed on to the second.
1) Isnt it request.getRequestDispatcher ? 2) I tried simulating the same - i did not get any exception when calling the second servlet without a doGet() method. The second servlet however, did get initialized. 3) When i included a doGet() method in the second servlet it(doGet() method) was called.