ServletRequest.getParameterMap () returns Map <String, String []> and ServletRequest.getParameter () returns String?

Can someone explain to me why ServletRequest.getParameterMap() returns a type

 Map<String, String[]> 

ServletRequest.getParameter() just returns a String type

I don’t understand why the map will ever display more than one value. TIA.

+47
java servlets
Dec 18 '09 at 14:54
source share
5 answers

It returns all parameter values ​​for controls named same .

For example:

 <input type="checkbox" name="cars" value="audi" /> Audi <input type="checkbox" name="cars" value="ford" /> Ford <input type="checkbox" name="cars" value="opel" /> Opel 

or

 <select name="cars" multiple> <option value="audi">Audi</option> <option value="ford">Ford</option> <option value="opel">Opel</option> </select> 

Any marked / selected values ​​will be displayed as:

 String[] cars = request.getParameterValues("cars"); 

It is also useful for several selections in tables:

 <table> <tr> <th>Delete?</th> <th>Foo</th> </tr> <c:forEach items="${list}" var="item"> <tr> <td><input type="checkbox" name="delete" value="${item.id}"></td> <td>${item.foo}</td> </tr> </c:forEach> </table> 

in conjunction with

 itemDAO.delete(request.getParameterValues("delete")); 
+53
Dec 18 '09 at 2:59 p.m.
source share
 http://foo.com/bar?biff=banana&biff=pear&biff=grape 

"biff" is now displayed on {"banana","pear","grape"}

+19
Dec 18 '09 at 14:57
source share

Real function to get all parameter values

  request.getParameterValues(); 

getParameter() is just a shortcut to the first one.

+9
Dec 18 '09 at 15:12
source share

In the case of multi-valued controls (checkbox, multi-select, etc.), request.getParameterValues(..) used to retrieve the values.

+3
Dec 18 '09 at 2:59 p.m.
source share

If you have a multi-valued control, for example a multi-part list or a set of buttons mapped to the same name, several selections will be displayed in the array.

+2
Dec 18 '09 at 2:56 p.m.
source share



All Articles