Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher to servlet

 
Radmika Arunachalam
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic