Reputation: 24767
I want to call servlet with some parameters and receive a response. The code is written in Java. What is the best (cleanest) way to do that?
Also, can i call a servlet and continue with the code withoud waiting the servlet to finish (close the connection and "forget about it")?
Upvotes: 6
Views: 30051
Reputation: 6442
For me this was the shortest and most useful tutorial about Apache HttpClient.
Upvotes: 0
Reputation: 365
You could use Apache HttpClient Apache HttpClient
This also has Non-blocking I/O functionality available NIO extensions
Here's a Tutorial for the Apache HttpComponents.
You could also try Jetty or Async Http Client
Upvotes: 2
Reputation: 154
Better use Apache HttpClient API for handling and communication with servlet
http://hc.apache.org/httpcomponents-client-ga/index.html
Features:
Upvotes: 4
Reputation: 340963
Example from here:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
From your perspective, servlet is just an URL on some server. As for not waiting for a response - read about Java threads. But you cannot close the HTTP connection without waiting for a servlet to finish as this might cause a servlet to fail. Simply wait for the response in a separate thread and discard it if it doesn't matter.
Upvotes: 4