Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top