Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

servlets

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

Map<String, String[]>  

ServletRequest.getParameter() just returns type String

I'm don't understand why the map would ever map to more then one value. TIA.

like image 354
BillMan Avatar asked Dec 18 '09 14:12

BillMan


People also ask

What is the return type of getParameter () method?

getParameter. Returns the value of a request parameter as a String , or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

What are the uses of ServletRequest?

ServletRequest allows the Servlet to access information such as: Names of the parameters are passed by the client. The protocol [scheme] such as the HTIP POST and PUT methods being used by the client. The names of the remote host that are made the request.

What is used of ServletRequest interface?

This interface is for getting data from the client to the servlet for a service request. Network service developers implement the ServletRequest interface. The methods are then used by servlets when the service method is executed; the ServletRequest object is passed as an argument to the service method.

What information does ServletRequest allow access to?

The ServletRequest interface allows the servlet access to information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it.


1 Answers

It returns all parameter values for controls with the same name.

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 checked/selected values will come in as:

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

It's also useful for multiple 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 combination with

itemDAO.delete(request.getParameterValues("delete")); 
like image 75
BalusC Avatar answered Sep 20 '22 00:09

BalusC