Reputation: 123
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();
}
Upvotes: 3
Views: 9454
Reputation: 3694
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.
Upvotes: 11
Reputation: 10727
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.
Upvotes: 1
Reputation: 40328
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.
Upvotes: 1