Sending a request from one servlet to another.


Web tier: servlets, JSP, Web frameworks: Sending a request from one servlet to another.

  1. Sending a request from one servlet to another. (3 messages)


    I have two servlets (S1 and S2) running on different machines (H1 and H2) respectively.

    S1 receives a request from a client (say an applet). Initially, S1 performs validation on this request.

    Then it sends this request to the servlet S2 on host H2.

    I have used the RequestDispatcher Interface but it doesn't work. The forward(req, res) hangs !!
    Any other options ? Also, what is the correct/best possible
    way of sending a request from S1 to S2 ?

    Thanks in advance,
  2. forward and include works only for URL located on the same machine.
    The redirect should works, else, you can use the class URL:

    String user="me";
    String password="secret";
    URL url;
    HttpURLConnection conn;
    url = new URL(");
    conn = (HttpURLConnection)url.openConnection();

    // Only if basic authentication is required
    String auth = "Basic " +new sun.misc.BASE64Encoder().encode((user + ":" + password).getBytes());
    conn.setRequestProperty("Authorization", " " + auth);

    out.print( conn.getResponseMessage());

    Should (may ?) works.
  3. Hi,

    I meanwhile ventured the option that you have mentioned, but so far I have not had success on that.

    to the second servlet, I have to set the attributes and headers of the first servlet.

    Somehow, that part seems to not function.

    Any leads on that would be helpful.

  4. If you are running servlets on two machines, then you have to have some web server installed on them. Also, I am assuming that both the systems have the web server listening on a port such as port 80.

    In this scenario, your servlets can be accessed using a URL which points to the servlet class folder.

    Use response.sendReDirect(url name).