3

In my servlet, req.getQueryString() returns null when an ajax request is sent to it. Is this because req.getQueryString() only works for GET and not POST?

public void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException {
req.getQueryString();
}

3 Answers 3

11

The easiest way to get hold of request parameters is to use request.getParameter(). This works for both GET and POST requests.

POST requests typically carry their parameters within the request body, which is why the request.getQueryString() method returns null.

Sign up to request clarification or add additional context in comments.

Comments

1

From docs:

This method returns null if the URL does not have a query string.

Since you are in a doPost() handler, we can assume that indeed the request has no query string since it is a POST.

Comments

1

POST request may have a query string, but this is uncommon. POST data is included directly after the HTTP headers that browser sends to the server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.