Tomcat并在请求中混合POST和GET参数

时间:2015-12-22 09:32:51

标签: java http tomcat

我已经阅读了问题:HTTP POST with URL query parameters并且明白可以这样做但是使用java和tomcat我无法管理它。

我有html页面:

<!DOCTYPE html>
<html>
<body>
<form action="HelloForm" method="POST">
    First Name: <input type="text" name="first_name">
    <br/>
    Last Name: <input type="text" name="last_name"/>
    <br/>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>

我发送http://localhost:8282/Hello.html?uri_param=pamparam点击提交按钮。

我通过代理跟踪了uri(GET like)和body(POST like)参数已被发送:

Referer: http://localhost:8282/Hello.html?uri_param=pamparam
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 36

first_name=Sergei&last_name=Rudenkov

但在null方法中执行request.getParameter("uri_param");时,我得到doPost

所以问题是:是否可以使用tomcat混合POST和GET参数?

已修改(已请求其他信息):

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>HelloForm</servlet-name>
        <servlet-class>HelloForm</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloForm</servlet-name>
        <url-pattern>/HelloForm</url-pattern>
    </servlet-mapping>
</web-app>

我的servlet:

public class HelloForm extends HttpServlet {

    String uri_param;

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {
        // Set response content type
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        String title = "Using GET Method to Read Form Data";
        String docType =
                "<!doctype html public \"-//w3c//dtd html 4.0 " +
                        "transitional//en\">\n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>First Name</b>: "
                + request.getParameter("first_name") + "\n" +
                "  <li><b>Last Name</b>: "
                + request.getParameter("last_name") + "\n" +
                "</ul>\n" +
                "<li><b>URI PARAM</b>: "
                + uri_param + "\n" +
                "</ul>\n" +
                "</body></html>");
    }

    // Method to handle POST method request.
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException, IOException {
        uri_param = request.getParameter("uri_param");
        doGet(request, response);
    }
}

结果: browser_view

1 个答案:

答案 0 :(得分:1)

我看到它的方式,您将GET参数添加到/HelloForm的调用中,而不是实际的表单 - 提交到<!DOCTYPE html> <html> <body> <form action="HelloForm?uri_param=janWasRight" method="POST"> First Name: <input type="text" name="first_name"> <br/> Last Name: <input type="text" name="last_name"/> <br/> <input type="submit" value="Submit"/> </form> </body> </html>

试试这个:

// Method to handle POST method request.
public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
        throws ServletException, IOException {
    uri_param = request.getParameter("uri_param");
    System.out.println("Parameter uri_param: " + uri_param);
    doGet(request, response);
}

也许

POST

修改

您甚至可以在屏幕截图中查看:您{{1}}编辑的网址中没有?uri_param