Java 在servlet的processRequest方法中发送POST请求

Java 在servlet的processRequest方法中发送POST请求,java,tomcat,servlets,post,Java,Tomcat,Servlets,Post,我有一个servlet,它接收请求中的一个参数,执行一些操作,然后它必须用JSON中的一些参数(包括接收到的参数)重定向(使用POST)到另一个服务器 我的代码是: protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Stri

我有一个servlet,它接收请求中的一个参数,执行一些操作,然后它必须用JSON中的一些参数(包括接收到的参数)重定向(使用POST)到另一个服务器

我的代码是:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
                              throws ServletException, IOException {

    String par = request.getParameter("myParameter");

    String par2 = someStuff();

    JSONObject json = new JSONObject();

    json.put("myParameter", par);
    json.put("otherParameter", par2);

    response.setContentType("application/json");
    response.getWriter().write(json.toString());

    ...
}
我想用以下内容替换这些点:

  • response.sendRedirect(…)
    方法,但它是一个GET请求

  • 使用
    forward
    方法的
    RequestDispatcher
    ,但我无法将请求发送到另一个容器

  • 如何使用JSON参数向另一个容器发送POST请求

    第一个解决方案(不可靠):

    在用户会话中设置
    JSON obj
    ,并将用户重定向到target
    cgi/path
    ,目标路径将从会话中找到JSON对象

    //in source servlet
    req.getSession().setAttribute("json_obj",json_obj);
    req.getSession().setAttribute("another_param",<<anotehr_param>>);
    //in the target servlet
    JSONObject json=(JSONObject)req.getSession().getAttribute("json_obj");
    
    对于第一个解决方案(不可靠):

    在用户会话中设置
    JSON obj
    ,并将用户重定向到target
    cgi/path
    ,目标路径将从会话中找到JSON对象

    //in source servlet
    req.getSession().setAttribute("json_obj",json_obj);
    req.getSession().setAttribute("another_param",<<anotehr_param>>);
    //in the target servlet
    JSONObject json=(JSONObject)req.getSession().getAttribute("json_obj");
    
    对于第一个解决方案(不可靠):

    在用户会话中设置
    JSON obj
    ,并将用户重定向到target
    cgi/path
    ,目标路径将从会话中找到JSON对象

    //in source servlet
    req.getSession().setAttribute("json_obj",json_obj);
    req.getSession().setAttribute("another_param",<<anotehr_param>>);
    //in the target servlet
    JSONObject json=(JSONObject)req.getSession().getAttribute("json_obj");
    
    对于第一个解决方案(不可靠):

    在用户会话中设置
    JSON obj
    ,并将用户重定向到target
    cgi/path
    ,目标路径将从会话中找到JSON对象

    //in source servlet
    req.getSession().setAttribute("json_obj",json_obj);
    req.getSession().setAttribute("another_param",<<anotehr_param>>);
    //in the target servlet
    JSONObject json=(JSONObject)req.getSession().getAttribute("json_obj");
    

    如果您必须能够联系其他服务器,那么您必须提交自己的POST请求。我建议你使用图书馆来做你的脏活。那么您的代码将如下所示:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    
      String par = request.getParameter("myParameter");
    
      String par2 = someStuff();
    
      JSONObject json = new JSONObject();
    
      json.put("myParameter", par);
      json.put("otherParameter", par2);
    
      HttpPost method = new HttpPost(new URI("https://host/service"));
      method.setHeader("Content-Type", "application/json");
      method.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
      HttpParams params=message.getParams();
      HttpConnectionParams.setConnectionTimeout(params, timeout);
      HttpConnectionParams.setSoTimeout(params, timeout);
      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(method);
      InputStream in = response.getEntity().getContent();
    
      // Do whatever you want with the server response
      // available in "in" InputStream
    
      ...
    }
    
    请注意,您需要添加大量错误处理,因为其中许多方法可能会失败(尤其是对
    HttpClient.execute
    )的调用),并且您需要为这些调用设置适当的超时,否则,在您联系另一个可能会束缚您的服务时,您的HTTP客户端会需要


    如果您发现JSON POST请求需要一段时间,您可以考虑将您的工作放在一个单独的线程中,并使用像Websocket这样的异步通信模型与客户机通信。您可能会发现,使用这样的策略,您将获得更好的服务器整体性能。

    如果您必须能够联系其他服务器,则必须提交您自己的POST请求。我建议你使用图书馆来做你的脏活。那么您的代码将如下所示:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    
      String par = request.getParameter("myParameter");
    
      String par2 = someStuff();
    
      JSONObject json = new JSONObject();
    
      json.put("myParameter", par);
      json.put("otherParameter", par2);
    
      HttpPost method = new HttpPost(new URI("https://host/service"));
      method.setHeader("Content-Type", "application/json");
      method.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
      HttpParams params=message.getParams();
      HttpConnectionParams.setConnectionTimeout(params, timeout);
      HttpConnectionParams.setSoTimeout(params, timeout);
      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(method);
      InputStream in = response.getEntity().getContent();
    
      // Do whatever you want with the server response
      // available in "in" InputStream
    
      ...
    }
    
    请注意,您需要添加大量错误处理,因为其中许多方法可能会失败(尤其是对
    HttpClient.execute
    )的调用),并且您需要为这些调用设置适当的超时,否则,在您联系另一个可能会束缚您的服务时,您的HTTP客户端会需要


    如果您发现JSON POST请求需要一段时间,您可以考虑将您的工作放在一个单独的线程中,并使用像Websocket这样的异步通信模型与客户机通信。您可能会发现,使用这样的策略,您将获得更好的服务器整体性能。

    如果您必须能够联系其他服务器,则必须提交您自己的POST请求。我建议你使用图书馆来做你的脏活。那么您的代码将如下所示:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    
      String par = request.getParameter("myParameter");
    
      String par2 = someStuff();
    
      JSONObject json = new JSONObject();
    
      json.put("myParameter", par);
      json.put("otherParameter", par2);
    
      HttpPost method = new HttpPost(new URI("https://host/service"));
      method.setHeader("Content-Type", "application/json");
      method.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
      HttpParams params=message.getParams();
      HttpConnectionParams.setConnectionTimeout(params, timeout);
      HttpConnectionParams.setSoTimeout(params, timeout);
      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(method);
      InputStream in = response.getEntity().getContent();
    
      // Do whatever you want with the server response
      // available in "in" InputStream
    
      ...
    }
    
    请注意,您需要添加大量错误处理,因为其中许多方法可能会失败(尤其是对
    HttpClient.execute
    )的调用),并且您需要为这些调用设置适当的超时,否则,在您联系另一个可能会束缚您的服务时,您的HTTP客户端会需要


    如果您发现JSON POST请求需要一段时间,您可以考虑将您的工作放在一个单独的线程中,并使用像Websocket这样的异步通信模型与客户机通信。您可能会发现,使用这样的策略,您将获得更好的服务器整体性能。

    如果您必须能够联系其他服务器,则必须提交您自己的POST请求。我建议你使用图书馆来做你的脏活。那么您的代码将如下所示:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    
      String par = request.getParameter("myParameter");
    
      String par2 = someStuff();
    
      JSONObject json = new JSONObject();
    
      json.put("myParameter", par);
      json.put("otherParameter", par2);
    
      HttpPost method = new HttpPost(new URI("https://host/service"));
      method.setHeader("Content-Type", "application/json");
      method.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
      HttpParams params=message.getParams();
      HttpConnectionParams.setConnectionTimeout(params, timeout);
      HttpConnectionParams.setSoTimeout(params, timeout);
      HttpClient client = new DefaultHttpClient();
      HttpResponse response = client.execute(method);
      InputStream in = response.getEntity().getContent();
    
      // Do whatever you want with the server response
      // available in "in" InputStream
    
      ...
    }
    
    请注意,您需要添加大量错误处理,因为其中许多方法可能会失败(尤其是对
    HttpClient.execute
    )的调用),并且您需要为这些调用设置适当的超时,否则,在您联系另一个可能会束缚您的服务时,您的HTTP客户端会需要


    如果您发现JSON POST请求需要一段时间,您可以考虑将您的工作放在一个单独的线程中,并使用像Websocket这样的异步通信模型与客户机通信。您可能会发现,使用这样的策略,您将获得更好的服务器整体性能。

    “response.sendRedirect(…)方法,但我无法发送参数。”为什么不呢?@BalusC实际上我可以将参数作为get请求的URL参数发送,但我想发送POST请求。(我编辑了这个问题,谢谢你指出我的错误。)你不能在这里发送一个普通的发帖请求吗?为什么必须使用这些servlet方法?可能会考虑让客户端先向其他服务器发送POST请求,然后获取请求,然后用更新后的参数向服务器发送请求,是否需要用户(从浏览器)发出请求,或者你想在服务器端处理帖子?@ChristopherSchultz我会在服务器端处理帖子。“response.sendRedirect(…)方法,但我无法发送参数。”为什么不呢?@BalusC实际上我可以将参数作为GET请求的URL参数发送,但我想发送帖子请求。(我编辑了这个问题,谢谢你指出我的错误。)你不能发个r吗