permaculture playing cards*
The moose likes Servlets and the fly likes Is it possible to call doGet/doPost method from RequestDispatcher.forward () Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is it possible to call doGet/doPost method from RequestDispatcher.forward ()" Watch "Is it possible to call doGet/doPost method from RequestDispatcher.forward ()" New topic
Author

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

Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
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

Joined: Jan 30, 2004
Posts: 91
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Nov 07, 2001
Posts: 312
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

Joined: Jan 30, 2004
Posts: 91
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to call doGet/doPost method from RequestDispatcher.forward ()
 
Similar Threads
Forwarding from doPost to doGet
How to make doPost as default service
Forwarding requests between servlets
Servlet to servlet
Call destroy() method inside the doGet() or doPost() Methods