Rohit Kolhey
Rohit Kolhey

Reputation: 57

How to send Response back to Servlet from another Servlet

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&param2=b&param3=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.
 }

Upvotes: 1

Views: 1951

Answers (1)

code_angel
code_angel

Reputation: 1537

Your Servlet2, the Request-Receiver, should behave normally:

  • get the request parameters
  • make something with them
  • generate the response
  • send it back

A basic example:

public class Servlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain; charset=UTF-8");
        response.getWriter().append("That is my response");
    }

}

Your Client, the Request-Sender, should process the response:

int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
    System.out.println("SUCCESS");
}
else {
    System.out.println("Response Code: " +  responseCode);
}

// may be get the headers
Map<String, List<String>> headers = connection.getHeaderFields();
// do something with them

// read the response body (text, html, json,..)
// do something usefull
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;

while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

Upvotes: 2

Related Questions