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 Servlet redirect to another server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet redirect to another server" Watch "Servlet redirect to another server" New topic
Author

Servlet redirect to another server

Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
Hello,

I am creating a shopping cart application and using paypal as payment gateway integration.

The usual way to checkout is put all the paypal variables in the form as hidden variables and post it to payapl.

For safety reasons, i was trying to post the checkout process to a servlet, intercepted by a filter where i create a custom request object inserting all the paypal values and pass the new request object to the servlet.

Now the servlet has the request object which contains all required paypal parameters.

Is there any way to forward the request to paypal from servlet or a filter.


SCJP 1.5, SCWCD 1.4.
Hanging between Web Services and EJB
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

If you are thinking about a forward like we do using request dispatcher, then that's not possible. You'll have to connect to Paypal using HttpsURLConnection or a API like Apache HttpClient to make a request to paypal...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
if i connect using Apache HttpClient, will the user be redirected to paypal site?

And the method which i am try to implement, is it fine or does it have some disadvantage
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Ramkumar Subburaj wrote:if i connect using Apache HttpClient, will the user be redirected to paypal site?

And the method which i am try to implement, is it fine or does it have some disadvantage


Do you want to redirect the client ot paypal or just make a request to paypal on client's behalf??
Ramkumar Subburaj
Ranch Hand

Joined: Sep 07, 2007
Posts: 83
I want to redirect the user to paypal site
Rukmal Dias
Greenhorn

Joined: Jun 13, 2011
Posts: 1
hi,

For this you need Apache HttpClient (http://hc.apache.org/downloads.cgi) libs. This makes tasks lot easier.

This is for HttpClient 4.1 library..



The POST method didn't work..
Hope this helps..

Rukmal Dias
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1086
    
    1

@Rukmal - Welcome to JavaRanch

Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
Yes it is possible. I have just implemented a code my application where my customer leaves my application site and goes to paypal page. Customer logs in and clicks "I agree" and gets back to my application/site.

I am using Spring framework. The first call that I mentioned above is where Paypal send me a token which I can use to actually charge the customer account. When customers reviews the final order on my site and decides to submit, I use this token and give a call to Paypal to charge the account.

How I am doing this:


Customer chooses option: I want to pay via Paypal. The call comes to my servlet.
Step 1. Get the paypal token. You will need to call the PayPal Api to get the token. Check Paypal developer guide.

Step 2. "Forward" customer to Paypal site by appending the token(REMEBER NEVER SEND PAYMENT INFORMATION IN THE URL.)

I fill in Paypal business value object (customer name, email, price, currecny etc.). Then I am forwarding the customer to this URL
String forwardUrl = https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + paypalResponseVO.getPayPalResponseVO().getApiToken();

Step3: After customer clicks agree on Paypal URL. Paypal automatically redirects the customer to my site. How is that possible. When you requested token number in step one, you pass information such as CancelUrl, SuccessUrl. Paypal uses this url to forward customer back to your site.

So now in my forward URL I have provided logic that if everything was fine then take the customer to the next page.

Step 4. Customer reviews the order and asks to charge the Paypal account. Use the same token number and send a another request to Paypal API to charge the account.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet redirect to another server