• 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

Is it possible to call doGet/doPost method from RequestDispatcher.forward ()

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a HttpServlet (HelloWorld) with doGet and doPost method. But both behave differently ( for example: doGet will print "Inside doGet HelloWorld" and doPost will print "Inside doPost HelloWorld"). Using RequestDispatcher if I want to call this two methods seperately. I think by default it will call the doGet method. To call the doPost method what should i have to do?
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/HelloWorld");
requestDispatcher.forward(request, response);

Thanks & Regards,
M.S.Raman
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. Not sure if that is possible, maybe someone else has a solutoin. As a work around simply call doPost(req, res) from your doGet method. Keeping all your logic in doPost.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are forwarding the entire request - the receiving servlet service method will look at the request to see if it is a GET or a POST. In order to change a request method, you have to change the request by wrapping it in a HttpServletRequestWrapper with a custom getMethod() that returns the method you want it to use.
Bill
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
You are forwarding the entire request - the receiving servlet service method will look at the request to see if it is a GET or a POST. In order to change a request method, you have to change the request by wrapping it in a HttpServletRequestWrapper with a custom getMethod() that returns the method you want it to use.
Bill


Thanks Louis Saha & William Brogden.
Regards,
M.S.Raman
 
Ben Dover
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but that still doesn't allow one to CHANGE the get method from what it was originally. Which is what I think the original issue was about. there is no setMethod() method in HttpServletRequestWrapper
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at 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