HTTPServletRequest getParameterMap() vs getParameterNames

后端 未结 1 418
心在旅途
心在旅途 2021-02-07 12:00

HTTPServletRequest req, has a method getParameterMap() but, the values return a String[] instead of String, for post data as

1条回答
  •  执笔经年
    2021-02-07 12:24

    If you are expecting pre determined parameters then you can use getParameter(java.lang.String name) method.

    Otherwise, approaches given above can be used, but with some differences, in HTTP-request someone can send one or more parameters with the same name.

    For example:

    name=John, name=Joe, name=Mia
    

    Approach 1 can be used only if you expect client sends only one parameter value for a name, rest of them will be ignored. In this example you can only read "John"

    Approach 2 can be used if you expect more than one values with same name. Values will be populated as an array as you showed in the code. Hence you will be able to read all values, i.e "John","Joe","Mia" in this example

    Documentation

    0 讨论(0)
提交回复
热议问题