Forums Register Login

Forwarding from doPost to doGet

+Pie Number of slices to send: Send
Hi,

I have a login servlet that, as the name would suggest, handles the login functionality of my webapp. If the login is approved, I'm currently forwarding to a static JSP page, but I would rather forward to another servlet that generates a list and let the user proceed from there.

The servlet to generate the list already works, as does the login servlet, it's just a matter of forwarding from one to the other.

The login servlet only has a doPost method, the doGet just returns the request and response objects. In the other servlet, we'll call MyServlet, it uses both doPost and doGet and the listing functionality exists within the doGet method.

Everytime I try to use the RequestDispatcher to forward to the servlet using my servlet mapping, I get an error. Is it possible to go from doPost to doGet? Or would I have to duplicate the list code inside the doPost method?
+Pie Number of slices to send: Send
No, the request type (GET, POST, etc...) is determined before your first servlet is ever called and is part of that request for the entire cycle.

A simple way to work with this is to combine your doPost and doGet methods (just have one call the other) so it won't matter which is called.

A more complex way of doing this is to wrap the request object by extending HttpRequestWrapper and overriding getMethod.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequestWrapper.html

If you add a setMethod or changeMethod method, you could change the requests method, and pass the wrapper to the next servlet as an argument to RequestDispatcher's forward method.
+Pie Number of slices to send: Send
Hi Ben,

Thanks for the advice, I'll check your suggestion out. I've never done something like that before and it sounds rather interesting.

Thanks,
Jason
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4391 times.
Similar Threads
With respect to servlets
jsp's hitting the ground running
jsp's hitting the ground running
loop in servlet(doPost)
Servlet to servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:43:02.