【问题标题】:How to send Response back to Servlet from another Servlet如何将响应从另一个 Servlet 发送回 Servlet
【发布时间】:2017-04-21 08:32:00
【问题描述】:

我编写了两个驻留在不同 Web 服务器上的 servlet。使用 java 中的 URL 对象从 Servlet1(Server1) 发送请求。并成功调用 Servlet2(server2)。但我还需要将响应从 Servlet2 发送回 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:

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

【问题讨论】:

  • 它与寻呼机重定向有何不同?
  • 现在很清楚问题出在哪里。一些代码总是有助于更好地理解。

标签: java servlets


【解决方案1】:

您的 Servlet2,即请求接收器,应该正常运行:

  • 获取请求参数
  • 和他们一起做点什么
  • 生成响应
  • 发回

一个基本的例子:

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

}

您的客户,即请求发送者,应处理响应:

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

【讨论】:

  • 这是我需要的最佳答案...感谢您的帮助...由于声誉较低,我无法投票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
相关资源
最近更新 更多