Search code examples
javahttprequest

Is there a way to get HTTP request parameter using wildcard?


We have a service that get values from formData in http requests.

Some parameters are packaging1_1 or packaging1_2 or packaging1_3 etc

We are using the following code to get the parameter value from HttpServletRequest request

String doseForm = request.getParameter("packaging1_0");

Is there any way to use the code with wildcard in the last number? e.g.

String doseForm = request.getParameter("packaging1_WILDCARD");

Solution

  • No, you can only get all the names and compare them on your own, like so:

    Set<String> packaging1Params = new TreeSet<>();
    for(Enumeration<String> names = request.getParameterNames(); names.hasMoreElements();) {
      String name = names.nextElement();
      if(name.startsWith("packaging1")) {
        packaging1Params.add(name);
      }
    } 
    

    And then get the values for all the filtered names.