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.
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")); http://foo.com/bar?biff=banana&biff=pear&biff=grape "biff" is now displayed on {"banana","pear","grape"}
Real function to get all parameter values
request.getParameterValues(); getParameter() is just a shortcut to the first one.
In the case of multi-valued controls (checkbox, multi-select, etc.), request.getParameterValues(..) used to retrieve the values.
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.