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 Forward to different URL and pass parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Forward to different URL and pass parameters

Pete Neu
Ranch Hand

Joined: Feb 18, 2005
Posts: 86
Hello,

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 ]
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi





I hope, it will help you. try this

bye for now
sat
ashok sashrith
Greenhorn

Joined: Jan 31, 2005
Posts: 12
try this,

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.
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
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
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 ]
Vijay Jagannathan
Greenhorn

Joined: Feb 17, 2003
Posts: 22
To elaborate on what Eddy said, use the c:redirect as follows:



Cheers
Vijay
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135

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.

Hope it works.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.

Thanks.
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Just glad it works.
mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
Hi Ben,
Can you explain how you did it??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

THis has already been covered in your other post.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Forward to different URL and pass parameters