Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...