Hi, I have this sample code. Is for a servlet that runs as client of another servlet.
URL url = new URL ("http://localhost/servlet/S1
HttpURLConnection httpConnection = (HttpURLConnection) url.getConnection();
httpConnection.setRequestMethod ("POST"); // I could be GET
PrintWriter pw = new printWriter (httpConnection.getOutputStream ());
pw.print ("key1=value1"); // I send this variable in the request
Object nextPage = httpConnection.getContent();
out.println ("Response Message = " + httpConnection.getResponseCode());
// If getResponseCode() returns 200, the HTTP communication was OK.
// After this, you will treat the response
InputStream is = httpConnetcion.getInputStream ();
InputStreamReader reader = new InputStreamReader (is);
BufferedReader stringReader = new BufferedReader (reader);
String line = stringReader.readLine();
Is important to set the HTTP request method , flush the OutputStream and get the content of response. Is not mandatory to send variables.