I have a question on redirecting to a servlet. I have noticed that sendRedirect() method would always end up in the doGet method of the redirected page. how do I make sendRedirect point to doPost of the redirected page.
I have heard of some work around like creating a jsp or html form that would forward the parameter. But is there a way to do this in servlets.
there is no direct way of sending a post request as the default method of request is get.also you cannot use response.sendredirect to send a post request.html form can be a solution to the problem.
Joined: Feb 22, 2010
Thank you Devaka and Archit.
Yeah, sendRedirect is really HTTP-GET. I still havent found another way to redirect to another page/servlet.
@Deveka: I need to do this because I want to pass a volume of parameters to the target(page to redirect) page. And making another jsp/html form would mean I have to touch JSP(View) side. I was thinking of handling everything in the Servlet(controller) side of the system. But if you have something in mind, I'd gladly like to hear from you.
It sounds as if both pages are part of the same web app, so why does it need to be a redirect? A server-side forward (using a RequestDispatcher) should work as well, and will also be faster.
Joined: Jul 10, 2010
even, i agree with your point.
if you want to use post over get just to hide your parameter name and value then request dispatcher is the best option for you as it uses get but does not allow the url to be seen in the browser. but if you have something else in your mind then you have to opt for jsp/html form.
The underlying reason is that POST requests send paramters in the body of the request. Redirect can not recreate the body.
Getting the original POSTed parameters to the servlet at the new URL depends on where that destination is located - in the same application? In the same servlet container? In the same local network? ... etc.
It seems I have the exact same requirement
.... I want to POST data tp an external servlet but do NOT want to use html <form>. I want to use server side code (like redirect)
I am integrating my web application with multiple external service providers (all providing the same type of service)
With most of these extenal systems, integration is pretty simple, standard and same
---> build a URL with required parameters (as provided in the service providers documentation)
........and in the code, simply invoke response.sendRedirect(toConstructedURL);
Only one entity does it in a different way (why? I am :confused
... they have provided me a sample HTML.
........The HTML is nothing but a <form> with a action, with method 'post'and multiple hidden fields within.
.........when the form is submitted, integration is achieved.
and so I want an server side code alternative to an html form submit. (I do not want to build a adapter for this one single thing)