1

I have read question: HTTP POST with URL query parameters and understood that it possible to do it but with java and tomcat I cannot manage it.

I have html page:

<!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>

And I send http://localhost:8282/Hello.html?uri_param=pamparam clicking on submit button.

I tracked by proxy that both uri(GET like) and body(POST like) params have been sent:

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

But I get null when execute request.getParameter("uri_param"); inside doPost method.


So the question is: Is it possible to mix POST and GET params using tomcat?

Edited(additional info was requested):

My 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>

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

Result: browser_view

Community
  • 1
  • 1
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192

1 Answers1

1

The way I see it, you added that GET-Parameter to the call to Hello.html, not to the actual Form-Submit to /HelloForm

Try this:

<!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>

And maybe do

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

Edit

You can even see that in your screenshot: There's no ?uri_param in the URL you POSTed

Jan
  • 13,738
  • 3
  • 30
  • 55