Meaningless Drivel is fun!
The moose likes Servlets and the fly likes redirect a post to another post at external URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "redirect a post to another post at external URL" Watch "redirect a post to another post at external URL" New topic

redirect a post to another post at external URL

Andrew Gibb Sr

Joined: Apr 12, 2012
Posts: 1
I have an httpservlet class, and I receive a post request in the doPost method of that response.

I want to basically do a response.sendRedirect(newURL) but I want to use the POST method instead of the GET method. I need to use POST because my parameters are larger than 2 KB, and that is the size limit for URLs on most browsers.

The newURL is an EXTERNAL URL on a different host, so from what I've heard I can't use a forward with the getrequestdispatcher method, because that only works within the current servlet's context.

I've seen some solutions using a proxy-type setup, where I would open up a new urlconnection within the servlet, and just funnel the results back to the response object. This seems like it would be a bit complex and I'm not sure it would work.

Is there any simple way of using the response object to redirect a POST request? Like using the flushbuffer method, or writing the parameters to the header or body...?
Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

I am not sure about this, but think you can use httpclient to do a post to the new URL.

SCJP 1.6,Preparing (Tryin to prepare) for scwcd
Tim Moores

Joined: Sep 21, 2011
Posts: 2413
A redirect always uses GET, no way around that. You could certainly do a programmatic POST to the remote server from within your servlet (no proxy needed for that, and the HttpClient library would indeed be useful for that), but then you'd have your URL in the client browser instead of the remote server's URL, and any user interaction with the resulting page -if any- would need to go through your servlet, essentially necessitating you to write a proxy. So it depends a bit on what the overall goal is.
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 128

agreed with Tim (Redirection is always a GET). Another option could be something like redirect the user to another URL even before displaying that form & let that application display the form. This is applicable only if you are the owner of second application as well. Otherwise, Tim has already answered it.
I agree. Here's the link:
subject: redirect a post to another post at external URL
It's not a secret anymore!