简体   繁体   English

如何将响应从另一个Servlet发送回Servlet

[英]How to send Response back to Servlet from another Servlet

I have written two servlet residing on different web server. 我已经在不同的Web服务器上编写了两个servlet。 Sending Request from Servlet1(Server1) using URL object in java. 使用Java中的URL对象从Servlet1(Server1)发送请求。 And successfully able to call the Servlet2(server2). 并成功调用了Servlet2(server2)。 But i need to send the response back to Servlet1 from Servlet2 also...How can i achieve that. 但是我也需要将响应从Servlet2发送回Servlet1 ...我该如何实现。 Please help me. 请帮我。

UPDATE UPDATE

Here is the code for test.... 这是测试代码。

Servlet1: 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: 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.
 }

Your Servlet2, the Request-Receiver, should behave normally: 您的Servlet2,即Request-Receiver,应正常运行:

  • 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM