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...?
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
Joined: Sep 21, 2011
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.
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.