GeeCON Prague 2014*
The moose likes Servlets and the fly likes sendRedirect as POST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect as POST" Watch "sendRedirect as POST" New topic
Author

sendRedirect as POST

Ian Cabigon
Greenhorn

Joined: Feb 22, 2010
Posts: 25
Hello Code Ranch,

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.

Page1Servlet


Page2Servlet


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.

Thanks,
Ian
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3112
    
  42

Generally, you cannot send a POST request using sendRedirect method. Why do you need doing that?
Moving to Servlets forum.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
archit thakur
Greenhorn

Joined: Jul 10, 2010
Posts: 24
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.
Ian Cabigon
Greenhorn

Joined: Feb 22, 2010
Posts: 25
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.

Ian
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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.
archit thakur
Greenhorn

Joined: Jul 10, 2010
Posts: 24
@Lester Burnham
even, i agree with your point.

@Ian
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.
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138

Is it possible for us to send a POST request using a link (<a href="#">Sent Post Request</a>) ?

Thanks,

Rahul
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
An HREF link will always use GET (unless it uses JavaScript to cause a form submit that uses POST).
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Lester Burnham wrote:An HREF link will always use GET (unless it uses JavaScript to cause a form submit that uses POST).


Yes, but then the form always uses POST method to submit, if we want to use the GET for some intention, then it will not be possible. Isn't it ?
archit thakur
Greenhorn

Joined: Jul 10, 2010
Posts: 24
@rahul
Yes, but then the form always uses POST method to submit, if we want to use the GET for some intention, then it will not be possible. Isn't it ?

NO BUDDY,
form can use both <form action="" method="post"><input type="submit"></form> will send a post request and <form action="" method="get"><input type="submit"></form> will send a get request
William Brogden
Author and all-around good cowpoke
Rancher

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

Bill
Jeevan Sunkersett
Ranch Hand

Joined: Jul 03, 2007
Posts: 77
Hi,

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)


thanks
Jeevan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

You'll either need to do it yourself with the classes in java.net, or use a 3rd-party tool such as HttpClient, to send the request.

java.net.URLConnection


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nilesh Pat
Ranch Hand

Joined: Mar 01, 2010
Posts: 46
i want to redirect to remote URL with some parameters in it's POST request....
Any idea???


Regards,
Nil
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

See the reply right above your own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect as POST