| Author |
RequestDispatcher to servlet
|
Radmika Arunachalam
Ranch Hand
Joined: Mar 29, 2004
Posts: 45
|
|
Suppost firstServlet implements doGet method and within that it calls RequestDispatcher forward/include to secondServlet and secondServlet implements doPost method. Will this scenario work? To understand clearly, code will be something like this By default forward/include to servlet means, which method it will call (according to spec)? anyone knows about it please help Thanks & Regards, Radmika SCJP, SCBCD
|
 |
Kedar Dravid
Ranch Hand
Joined: May 28, 2004
Posts: 333
|
|
Suppost firstServlet implements doGet method and within that it calls RequestDispatcher forward/include to secondServlet and secondServlet implements doPost method. Will this scenario work? To understand clearly, code will be something like this code: -------------------------------------------------------------------------------- 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>"); -------------------------------------------------------------------------------- By default forward/include to servlet means, which method it will call (according to spec)? anyone knows about it please help
It will give an exception: HTTP method GET is not supported by this URL. RequestDispatcher always executes the doGet() method of the target servlet.
|
 |
Karthik Rajendiran
Ranch Hand
Joined: Aug 13, 2004
Posts: 209
|
|
Hello, 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. I hope you have got clear
|
SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
|
 |
PNS Subramanian
Ranch Hand
Joined: Jul 13, 2004
Posts: 150
|
|
A couple of things here 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. Env : Tomcat 5.0.30/Win XP
|
 |
Radmika Arunachalam
Ranch Hand
Joined: Mar 29, 2004
Posts: 45
|
|
Kedar, Karthik, and Subramanian Thanks to u all. Now i understood that it is based on from which method u are invoking RequestDispatcher. Sorry for the C&P error, it should be request not response... Subramanian, did u check the TOMCAT log whether it posts any error/exception there.. Once again Thanks to all..
|
 |
 |
|
|
subject: RequestDispatcher to servlet
|
|
|