File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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
Author

redirect a post to another post at external URL

Andrew Gibb Sr
Greenhorn

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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: 124

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: http://aspose.com/file-tools
 
subject: redirect a post to another post at external URL
 
Similar Threads
sending back a URL
Choosing Between Forward and Redirect
redirecting the request to URL
Redirecting to an outside URL from HandlerInterceptor
jsp:forward and res.sendRedirect()