user2760273
user2760273

Reputation: 123

Java HttpServletRequest getquerystring

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

Answers (3)

rcgeorge23
rcgeorge23

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

user987339
user987339

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

Nikos Paraskevopoulos
Nikos Paraskevopoulos

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

Related Questions