配置tomcat接受post请求

发布于 2024-10-19 19:24:23 字数 1535 浏览 2 评论 0原文

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

这是我的方法,它将 post 请求发送到 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();
    }

        }

谢谢

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 ?

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

 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走走停停 2024-10-26 19:24:23

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

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

它应该是

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

? 仅当您将其作为 GET 查询字符串连接到请求 URL 时才有效。当您想将其编写为 POST 请求正文时,不应使用它。

也就是说,如果该服务不应该返回 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);

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

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

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

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

无论哪种方式,您都应该能够通过读取 URLConnection#getInputStream() 来获取结果(响应正文)。

另请参阅:


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

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.

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.

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.

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

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

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

Either way, you should be able to get the result (the response body) by reading the 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.

岁月苍老的讽刺 2024-10-26 19:24:23

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

如果您从头开始安装 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>

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

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:

 <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文