Retrieve POST parameters only (Java)

前端 未结 6 2061
深忆病人
深忆病人 2021-01-04 10:03

Does anyone know of a way to get only POST parameters from an HttpServletRequest object?

IE, PHP has the $_POST superglobal and Perl\'s CGI.pm will only retrieve POS

6条回答
  •  青春惊慌失措
    2021-01-04 10:36

    From my understanding, there are no such things as POST parameters and GET parameters in HTTP, there are POST and GET methods. When a request is made using the POST method, parameters go within the message body. In case of a GET request, parameters go in the URL.

    My first thought was that it was an implementation bug in your servlet container. But, since things are not always as you expect, java servlet specification (at least the 2.4 version) does not differentiate between the two kind of parameters. So, there is no way to obtain post or url parameters using the servlet API.

    Surely you already have a plan B. But, just in case, I post two alternatives that came to my mind:

    1. If you have access to the parameter name definition, you could use a prefix to differentiate between the two when you iterate the getParameterNames() result.

    2. You could parse the URL creating an URL object and using getQuery() method to obtain just the parameters. Then, parse the parameters on the query string using some utility class such as ParameterParser in HttpClient library. And finally, subtract that names from the getParameterNames() result.

提交回复
热议问题