12

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 POST parameters if the HTTP method is POST (by default).

HttpServletRequest.getParameter(String) will include the GET URL parameters even if the HTTP method is POST.

dankuck
  • 198
  • 1
  • 1
  • 11

6 Answers6

7

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.

df.
  • 129
  • 1
  • 6
  • Hmm... I am just testing one implementation (Tomcat for Win32). Have you tested any others? – dankuck Jul 29 '09 at 03:45
  • 1
    ParameterParser sounds like the perfect package for the task of identifying URL parameters and values. Then something like the mechanism in ykaganovich's answer could be used. – dankuck Jul 30 '09 at 01:16
6

I guess one way might be to manually parse HttpServletRequest.getQueryString() and check that a parameter is not present in it.

A naive implementation (ignoring url-escaped key values) would go something like this (untested) :

public boolean isInQuery(HttpServletRequest request, String key) {
  String query = request.getQueryString();
  String[] nameValuePairs = query.split("&");
  for(String nameValuePair: nameValuePairs) {
    if(nameValuePair.startsWith(key + "=")) {
      return true;
    }
  }
  return false;
}
ykaganovich
  • 14,736
  • 8
  • 59
  • 96
  • 1
    That is a really simple solution. Still, this sounds like a workaround. I wonder if there is some standard class to do the work. For readers, add this line at the beginning of the method: key = URLEncoder.encode(key, "UTF-8"); – dankuck Jul 29 '09 at 03:51
  • Ah, I guess "workaround" is the wrong word. It just feels wrong because I know there is code doing something very similar hidden somewhere in there. – dankuck Jul 29 '09 at 04:11
  • 1
    df mentions a class called ParameterParser in Apache's HttpClient class. It's not in Java's standard library, but at least it thinks of all the little things we might have forgotten here. Then the code could go through getParameterValues() and ignore any that have one of the values given by ParameterParser.parse(). – dankuck Jul 30 '09 at 01:26
  • @ViktorBrešan Yes, which is why this can be used to solve the OP's problem. What's your point? – ykaganovich Nov 14 '11 at 18:26
  • @ykaganovich Sorry, my mistake. – Viktor Brešan Nov 16 '11 at 16:32
  • When a parameter is in both the query and the post parameters this method won't be able to detect that there's also a parameter in the post parameters – Jimmy T. May 24 '19 at 20:13
3

Couldn't you just get the parameters from the HttpServletRequest within doPost or doGet in a subclass of HttpServlet?

Anything you grab (via getParemeter) inside of doPost is a POST and anything inside of doGet is a GET.

seth
  • 36,759
  • 7
  • 60
  • 57
  • I'm trying to ignore the parameters that are passed in the URL. Those are included by getParameter no matter which HTTP method is used. – dankuck Jul 29 '09 at 14:20
  • 1
    Post requests can have both GET and POST parameters. If only GET parameter (say "nickname") is present, then if you do request.getParameter("nickname") inside doPost, you would get "nickname" GET parameter, which should be null from your explanation. – Jamol Aug 26 '11 at 06:00
2

I think you could do something with getMethod() available from the HttpServletRequest Interface.

Java doc 1.6

This is also available in 1.4 and 1.5.

  • That won't tell you anything about the source of the parameters. It will simply tell you which HTTP method was used in the request. – Yinzara Apr 06 '15 at 23:07
1

I'm not sure if this would work, but you could try extracting the raw content of the POST body using request.getReader(). The container may remove that data before handing control to your application, though, and even if it didn't, you'd have to decode the parameter string yourself.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Funny enough, I'm more inclined to use the round-about ideas suggested above because the POST data has at two different common formats (and any number of others). – dankuck Jul 30 '09 at 01:31
1

The question was answered in this related post:

Normaly you can GET and POST parameters in a servlet the same way:

request.getParameter("cmd");

But only if the POST data is encoded as key-value pairs of content type: "application/x-www-form-urlencoded" like when you use a standard HTML form.

Community
  • 1
  • 1
AlcaDotS
  • 339
  • 4
  • 8