1

In Java servlets you read a JSON from a POST request e.g. via

new JSONObject(toString(httpRequest.getInputStream()))

Now additionally to the JSON I would like to specify parameters in the URL, they can be read via:

httpRequest.getParameterMap().get("someURLParam")

All is working (I'm using AJAX post requests and jetty for server side)

BUT

I'm concerned and confused if and when these two methods influence each other as the javadocs from javax.​servlet.​ServletRequest.getParamter(String) says:

If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via ServletRequest.getInputStream or ServletRequest.getReader can interfere with the execution of this method.

What does it mean in my case? Or do they only interfere if content type is x-www-form-urlencoded? Or only if using getParameter and the method getParameterMap is fine?

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • I've looked into the jetty sources and they indeed only call getInputStream within getParameterMap if: form encoded content && not already read && (put or post). But is this the case for all containers where is this specified? – Karussell Mar 27 '15 at 13:53
  • This answer says that is it only for urlendoded post data, but doesn't support the statement with any official docs... http://stackoverflow.com/a/3831791/438742 – kan Mar 27 '15 at 14:01
  • The answer there is a bit confusing as I can obviously read the parameter without the form-encoded type – Karussell Mar 27 '15 at 14:09
  • How do you can? Even if the POST body is not form-urlencoded? – kan Mar 27 '15 at 14:36

1 Answers1

0

If you are only using getParameter/getParameterMap, you will be fine. This is because, behind the scenes, those methods may call getInputStream. The spec says MAY because it's up to the implementation, so the behavior may vary from one container to another.

If your content isn't form encoded, or you are processing a GET request, etc., getParameter/getParameterMap only needs to get the parameters from the query string, so it makes sense that Jetty wouldn't read the body in those cases.

Lawrence McAlpin
  • 2,745
  • 20
  • 24