1

Let's assume I have a form with these fields (HTML)

<input type="text" name="user[name]" value="name"/>
<input type="text" name="user[email]" value="email"/>
<input type="text" name="user[age]" value="age"/>

how do I get this information inside a java Servlet?

In php it is just

      $_POST["user"]["name"]

or

      $_GET["user"]["name"]

I am not referring at simple field like

      <input type="text" name="user" value="jimmy"/>

I am referring at retrieving arrays of data, like in the case of a set of checkboxes or a multiple select.

2 Answers 2

1

One thing to keep in mind, is that the brackets in the field names have no special meaning and are simply part of the name, so you'd have to include them when using ServletRequest::getParameter:

request.getParameter("user[name]");

It's only PHP (or another additional framework), that "automagically" treats names with brackets as an array (or in case of Java more likely a Map).

If you what such a map and you are not using a such framework you'd need to create it yourself, for example, by looping over ServletRequest::getParameterNames.

Example:

public static Map<String, String> getParameterMap(ServletRequest request, String mapName) {

  Map<String, String> result = new HashMap<String, String>();     

  Enumeration<String> names = request.getParameterNames();
  while (names.hasMoreElements()) { 
    String name = names.getNextElement();
    if (name.startsWith(mapName + "[") && name.endsWith("]")) {
      result.put(name.substring(mapName.length()+1, name.length() - 2), request.getParameter(name));
    }
  }

  return result;
}

This is just a quick (untested) example. It doesn't support multiple level maps (user[name][last]) nor multiple values for one name (which need getParameterValues).

Sign up to request clarification or add additional context in comments.

1 Comment

Ok. But how can I get the data for a set of checkboxes or a multiple select?
1

in your java servlet sub class, you override doGet and doPost,

  class subServlet extends HttpServlet{
      public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
         request.getParameter("paraname");
      }

      public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
         request.getParameter("paraname");
      }
  ....
  }

update: use getParameterMap to return a map of params.

2 Comments

I know how to get a request parameter, but I expected that request.getParameter("user"), for my case would give me a Map,HashMap, List,Array whatever. I do not want to write request.getParameter("user[name]").
you can use getParameterMap more details are here docs.oracle.com/javaee/5/api/javax/servlet/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.