Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • 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
 
Ben Dover
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic