How to submit form data to external service and redirect user from Java

I need help with sending POST data to an outside website and making the user’s browser go to that site. Right now I’m using HTTP client libraries to send the data, but the user stays on my page instead of being taken to the external site.

I want the user to end up on the payment page after I send the required parameters. Here’s what I’m working with:

try {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost request = new HttpPost("https://checkout.example.com/process");
    
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("amount", "750"));
    params.add(new BasicNameValuePair("orderId", "12345"));
    
    request.setEntity(new UrlEncodedFormEntity(params));
    
    CloseableHttpResponse response = client.execute(request);
    
    if (response.getStatusLine().getStatusCode() == 200) {
        // User should be redirected to external payment site here
        // But they stay on current page
    }
    
    response.close();
    client.close();
    
} catch (IOException e) {
    e.printStackTrace();
}

How can I make the browser actually navigate to the external URL after posting the form data?

hold up - ur not sending payment info through a GET redirect, are u? that’s not secure & most payment gateways won’t allow it. check the external service docs 1st. do they support token-based redirects instead of raw payment data?

u cant redirect from server-side java like that. the http client sends the request but won’t change what happens in the user’s browser. try making an html form that submits directly to the external site, or use response.sendRedirect() after ur logic runs.

You’re running the HTTP request server-side but expecting it to work client-side. When your Java backend sends that POST with HttpClient, it gets the response but can’t control where the user’s browser goes. Here’s what works: build an HTML form dynamically and auto-submit it. Create a response page with a form that has hidden fields for your parameters, then use JavaScript to automatically submit it to the external service. This way the user’s browser actually makes the POST request and follows any redirects from the payment provider. Or if the external service allows it, do your server-side processing first, then redirect the user to a URL with the parameters as query strings instead of POST data.