【问题标题】:How to send request and proceed response, and send back to Client?如何发送请求并进行响应,然后发送回客户端?
【发布时间】:2014-09-13 00:34:53
【问题描述】:

客户端通过 HTTP 请求(通过浏览器,post)调用 Servlet,然后 Servlet 应该向外部网站发送请求(get),并从网站接收响应(post)。 Servlet 处理响应并向客户端发送响应(发布)。

我的问题是如何在 Servlet 中发送和接收请求/响应并将某些内容发送回客户端?

【问题讨论】:

  • 你是说网络服务??
  • 与外部网站我的意思是像 ebay 这样的普通网络服务,它会在 html f.E. 中返回一个帖子响应

标签: java servlets request response


【解决方案1】:

您可以在此表单中使用请求的集合属性request.setAttribute(String key, Object value)

例子:

public class FindPerson extends HttpServlet {

    // ... doGet implementation

    @Override
    protected void doPost( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {

        // data to send to the client
        String name = "John White";
        int age = 54;



        // Adding attributes to the request
        request.setAttribute( "personName", name );
        request.setAttribute( "personAge", age );

        // Sending the result to the.jsp page
        getServletContext().getRequestDispatcher( "/WEB-INF/result.jsp" ).forward( request, response );

        }
    }
}

在此之后,您可以使用 JSTL 在 JSP 页面上读取这些数据:

<p>Name: ${ requestScope.personName } </p>
<p>Age: ${ requestScope.personAge } </p>

其中personNamepersonAge 是地图的。您使用request.setAttribute 设置它们。

!!!已更新!!!

在您的情况下,您将调用其他 servlet,这些 servlet 将对外部站点执行请求等。所有这些(从外部站点收集数据 + 处理此数据)都将被放置而不是设置值的代码姓名和年龄(见上例)。 希望对您有所帮助!

【讨论】:

    【解决方案2】:

    您可以先创建 URL,然后使用 URLConnection 对象连接并接收响应,如下 GET 请求/响应

    URL url = new URL(urlString);
    HttpURLConnection c = (HttpURLConnection)url.openConnection();  //connecting to url
    c.setRequestMethod("GET");
    BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));  //stream to resource
    String str;
    while ((str = in.readLine()) != null)   //reading data
       responsestring += str+"\n";//process the response and save it in some string or so
    in.close();  //closing stream
    response.getWriter().write(responsestring);
    

    更新 对于 POST 请求/响应,请执行此操作

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    
    con.setRequestMethod("POST");
    
    String urlParameters = ..;
    
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();
    
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer res = new StringBuffer();
    
    while ((inputLine = in.readLine()) != null) {
        res.append(inputLine);
    }
    in.close();
    //process response
    response.getWriter().write(res);
    

    【讨论】:

      【解决方案3】:
      URL url = new URL(urlString);
      HttpURLConnection c = (HttpURLConnection)url.openConnection();  //connecting to url
      c.setRequestMethod("GET");
      BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));  //stream to resource
      String str;
      while ((str = in.readLine()) != null)   //reading data
          responsestring += str+"\n";//process the response and save it in some string or so
      in.close();  //closing stream
      response.getWriter().write(responsestring);
      

      【讨论】:

        猜你喜欢
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多