I have written two servlet residing on different web server. Sending Request from Servlet1(Server1) using URL object in java. And successfully able to call the Servlet2(server2). But i need to send the response back to Servlet1 from Servlet2 also...How can i achieve that. Please help me.
UPDATE
Here is the code for test....
Servlet1:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside MyServlet.");
String urlParameters = "param1=a¶m2=b¶m3=c";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
String requestURL = "http://localhost:8080/Application2/Servlet2";
URL url = new URL( requestURL );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty( "charset", "UTF-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
ObjectOutputStream out = new ObjectOutputStream(conn.getOutputStream());
out.write( postData );
conn.connect();
}
Servlet2:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside CallToAuthorize.Getting Access Token.");
//do something here and send the response back to Servlet1.
//Primarily i will be sending String back to Servlet1 for its request.
}