I would like to forward the user from my web app to a different one. I need to pass some parameters with "POST" to a different site(http:/foo.com). The requestDispatcher only takes context relative paths and the response.redirect does not allow POST or any data to be sent.
How can I do this? [ October 20, 2005: Message edited by: Pete Neu ]
Forward your request to a temporary html within your application. And while loading the html(in onload), forward it again to the different site (http:/foo.com) and retrieve your data.
If you are using JSP technology, try to use the JSP JSTL core tag <c:redirect>
Make sure you have the JSTL Library in your class path.
Otherwise, if you using Servlet to do the cross context redirection, I believe that best is to create the url yourself like the following:
SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Pete Neu
Ranch Hand
Joined: Feb 18, 2005
Posts: 86
posted
0
Thanks for the many answers. The problem I still can't solve is that I need to do a POST response. Meaning: I don't want the user to see that I return the parameters to the other server. Elsewhise the user could manipulate them or destroy them
[ October 20, 2005: Message edited by: Pete Neu ] [ October 20, 2005: Message edited by: Pete Neu ]
You can't redirect with a post response. The requestDispatcher.forward method only works within your context. The response.sendRedirect method works by sending a 302 response header to the browser (which causes it to make a new GET request).
You could make the post from the server using either URLConnection or Apache's HttpClient, get the results, and then redirect to the other site.
Or
This is a little more of a hack (because it depends on Javascript at the browser side). Redirect to a page of your own that has the form in it (with the action pointing to the other site) and use the Javascript onload event to submit the form.
The second approach may cause popup warnings that the page is trying to take you to a different site; especially if you're under SSL.
Thanks for the many answers. The problem I still can't solve is that I need to do a POST response. Meaning: I don't want the user to see that I return the parameters to the other server.
Sorry, I misunderstood your requirement at first.
Well, it's possible to redirect the POST request. Check out the following:
This will use HTTP 307 status code to redirect your request with it's Body content. sendRedirect uses other code to redirect, which cause the Body to be discarded. This method works under IE, but i'm not sure how other browser will handle this 307 code.
Eddy, That's a great tip. I just tried it with Firefox and it worked.
The browser did warn me with a popup message saying "This web page is being redirected to a new location. Would you like to resend the form data that you have typed to the new location?" but that's to be expected. I didn't try under SSL which tends to have it's own layer of warnings.