• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher to servlet

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic