Servlet get GET and POST's parameters at the doPost method

My problem is when I'm trying to access a POST Variable with request.getParameter("name") , it works perfectly. But in some conditions, when a POST request arrives at my application, I also need to get GET Parameter from the Query String.

As far as I can see, with getParameter , you can only access current request's parameters, but, as in my condition, as I said, I also need to fetch GET Parameters inside doPost method.

Is there a way to fetch GET Parameters without parsing the Query String?


The getParameter() method can return (if possible) both GET and POST parameters as it works transparently between GET and POST . You don't need to do any explicit work to get the GET parameters. you can use getParameter for both query parameters and POST parameters.

But should you do it? - It's considered a poor design practice especially if there is sensitive information to be sent.

Take a look at this answer:

  • HTTP POST with URL query parameters -- good idea or not?

  • If you have parameters with the same name in the query string and in the posted form data, use getParameterValues() .

    Example:-

    String fromQuery = request.getParameterValues("name")[0];
    String fromForm = request.getParameterValues("name")[1];
    

    I think you have a confusion here. You can retieve all the request parameters (in both GET or POST or others) using the same getParameter(..) depending upon the type of request. If it's a GET request, you can retrieve all the GET parameters. If it's a POST request, you can retrieve all the POST parameters. You get parameters using getParameter(...) . And you make one request at a time. If you make a POST request in html or JSP file, you use doPost method receive all the parameters. At this point, there is nothing in GET request. Then after that, you make a GET request, you retrieve all the parameters in doGet method. At this moment, There is nothing in POST. Remember, HTTP requests are stateless.

    链接地址: http://www.djcxy.com/p/41100.html

    上一篇: struts2 s:form元素修剪action属性中的s:url参数

    下一篇: Servlet在doPost方法中获得GET和POST的参数