java 配置tomcat接受post请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5122531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:33:55  来源:igfitidea点击:

Configure tomcat to accept post request

javajsptomcatservlets

提问by blue-sky

How can I configure tomcat so when a post request is made the request parameters are outputted to a jsp file? Do I need a servlet which forwards to a jsp or can this be handled within a jsp file ?

如何配置 tomcat,以便在发出 post 请求时将请求参数输出到 jsp 文件?我是否需要一个转发到 jsp 的 servlet 或者这可以在 jsp 文件中处理吗?

Here is my method which sends the post request to the tomcat server -

这是我将发布请求发送到 tomcat 服务器的方法 -

 public void sendContentUsingPost() throws IOException {

        HttpConnection httpConn = null;
          String url = "http://LOCALHOST:8080/services/getdata";
     //   InputStream is = null;
        OutputStream os = null;

        try {
          // Open an HTTP Connection object
          httpConn = (HttpConnection)Connector.open(url);
          // Setup HTTP Request to POST
          httpConn.setRequestMethod(HttpConnection.POST);

          httpConn.setRequestProperty("User-Agent",
            "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
          httpConn.setRequestProperty("Accept_Language","en-US");
          //Content-Type is must to pass parameters in POST Request
          httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");


          // This function retrieves the information of this connection
          getConnectionInformation(httpConn);

                  String params;
          params = "?id=test&data=testdata";
                      System.out.println("Writing "+params);
          //            httpConn.setRequestProperty( "Content-Length", String.valueOf(params.length()));

          os = httpConn.openOutputStream();

          os.write(params.getBytes());

 } finally {
          if(os != null)
            os.close();
      if(httpConn != null)
            httpConn.close();
    }

        }

Thanks

谢谢

回答by BalusC

First of all, your query string is invalid.

首先,您的查询字符串无效。

params = "?id=test&data=testdata";

It should have been

应该是

params = "id=test&data=testdata";

The ?is only valid when you concatenate it to the request URL as a GET query string. You should not use it when you want to write it as POST request body.

?当你把它串联请求URL作为GET查询字符串才有效。当您想将其写为 POST 请求正文时,您不应该使用它。

Said that, if this service is not supposed to return HTML (e.g. plaintext, JSON, XML, CSV, etc), then use a servlet. Here's an example which emits plaintext.

也就是说,如果这个服务不应该返回 HTML(例如纯文本、JSON、XML、CSV 等),那么使用 servlet。这是一个发出纯文本的示例。

String id = request.getParameter("id");
String data = request.getParameter("data");
response.setContentType("text/plain");
response.setContentEncoding("UTF-8");
response.getWriter().write(id + "," + data);

If this service is supposed to return HTML, then use JSP. Change the URL to point to the JSP's one.

如果该服务应该返回 HTML,则使用 JSP。更改 URL 以指向 JSP 的 URL。

String url = "http://LOCALHOST:8080/services/getdata.jsp";

And then add the following to the JSP template to print the request parameters.

然后将以下内容添加到 JSP 模板中以打印请求参数。

${param.id}
${param.data}

Either way, you should be able to get the result (the response body) by reading the URLConnection#getInputStream().

无论哪种方式,您都应该能够通过阅读URLConnection#getInputStream().

See also:

也可以看看:



Unrelated to the concrete problem, you are not taking character encoding carefully into account. I strongly recommend to do so. See also the above link for detailed examples.

与具体问题无关,您没有仔细考虑字符编码。我强烈建议这样做。另请参阅上面的链接以获取详细示例。

回答by Ajay Takur

A servlet can handle both get and post request in following manner:

servlet 可以通过以下方式处理 get 和 post 请求:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   //remaning usedefinecode
    } 

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

If you have a tomcat installation from scratch, don't forget to add the following lines to web.xml in order to let the server accept GET, POST, etc. request:

如果你从头开始安装tomcat,不要忘记在web.xml中添加以下几行,以便让服务器接受GET、POST等请求:

 <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>

    ...

    <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
    </init-param>

   ...

</servlet>