1

I have a java servlet with a URL Query string with instructions like this

http://hostname/servet?param1=value1&param2=value2

I also structure the doPost/doGet like this

public void doPost(HttpServletRequest req, HttpServletResponse res) {
        try {
            doGet(req, res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public void doGet(HttpServletRequest req, HttpServletResponse res) {
        try {
            String sParam1 = req.getParameter("param1")
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I can access each queryString parameters via getParameter() for GET actions. But when I attempt to access the same queryString via getParameter() for POST actions, the returned value is NULL.

So, I would like to confirm this behaviour of getParameter for POST and GET actions. That is getParameter does NOT return queryString parameters for POST actions ? And do I need to manually dissect a query string to process them in cases of a POST action ?

angryITguy
  • 9,332
  • 8
  • 54
  • 82
  • I can see that it's related, but I am looking for a specific response to this question in relation to managing servlet parameters. Link is helpful though. – angryITguy Jul 23 '13 at 03:04
  • "do I need to manually dissect a query string to process them in cases of a POST action", In case of post, they will not be appended to URL, so no way of dissecting. – kosa Jul 23 '13 at 03:07
  • Ah.. So it's ignored ? Were older implementations of servlet containers ever returning the querystring ? #confused – angryITguy Jul 23 '13 at 03:08
  • This question has been flagged as a duplicate, but I disagree. The related question does not explicitly answer my question here about the behaviour of doGet and doPost for getParameter and whether a querystring is still accessible from a POST transaction. Have requested that it be re-opened. – angryITguy Jul 24 '13 at 04:05

1 Answers1

1

For GET method, parameters are sent as part of the URL (the query string), for POST method parameters are sent as part of the body, that's why in the POST case you don't get the parameters, as they are searched in the body not in the URL.

do I need to manually dissect a query string to process them in cases of a POST action ?

Yes, if you are in the case where you are sending a query string but using method POST, you'll have to parse the query string by yourself, unless you honor the standards and send parameters inside the body rather than in the URL.

morgano
  • 17,210
  • 10
  • 45
  • 56
  • I have updated my question, please feel free to review your answer. – angryITguy Jul 23 '13 at 03:05
  • Did getParameter ever retrieve querystring values for post actions ? I am working on some very old code and it stopped working recently. – angryITguy Jul 23 '13 at 03:19
  • The [JavaEE API](http://docs.oracle.com/javaee/7/api/) doesn't specify what happens if you use getQueryString() and you are not using a GET method, I guess you'll get a null value, but have a try, maybe I'm wrong – morgano Jul 23 '13 at 03:23
  • The reason I asked about the behaviour of POST with getParameter is that I am working with the IBM JVM, and older versions actually get the Querystring parameters from a POST with getParameter, recent versions (ie 1.6) now enforce a rule consistent with Oracle's (nee Sun's) JVM. Perhaps IBM's JVM was a bug, or just non-conforming. – angryITguy Jul 24 '13 at 04:07
  • @giulio: My guess is it could be non conforming. Each of these implementer have their own features added on top of spec. – kosa Jul 24 '13 at 04:17