Karussell
Karussell

Reputation: 17375

Reading URL parameters when POST request with data arrives in Java Servlet

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?

Upvotes: 1

Views: 2028

Answers (1)

Lawrence McAlpin
Lawrence McAlpin

Reputation: 2785

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.

Upvotes: 0

Related Questions