Example of Map of the returned value of request. getParameterMap in java

Source: Internet
Author: User

Request. the returned type of getParameterMap () is a Map-type object, that is, the corresponding relationship of key-value. However, note that the type of value is String [], not String.

It is easy to obtain the parameters submitted on the jsp page, but it can be used to convert the parameters and values in the request into a map. The following is a map structure formed by printing the obtained parameters and values: map (key, value []), that is, the key is a String type, and the value is a String type array.
For example:
The form request contains the multi-choice box checkbox or the parameter t1 = 1 & t1 = 2 & t2 = 3 in the request.
Map Structure:

The Code is as follows: Copy code
Key = t1; value [0] = 1, value [1] = 2
Key = t2; value [0] = 3

If map. get ("t1") is used directly, the result is: Ljava. lang. String; value is only in the array format, which prevents the same parameter name.

Example

The Code is as follows: Copy code

/**
* Obtain the parameter Map from the request and return a readable Map.
*
* @ Param request
* @ Return
*/
@ SuppressWarnings ("unchecked ")
Public static Map getParameterMap (HttpServletRequest request ){
// Parameter Map
Map properties = request. getParameterMap ();
// Return value Map
Map returnMap = new HashMap ();
Iterator entries = properties. entrySet (). iterator ();
Map. Entry entry;
String name = "";
String value = "";
While (entries. hasNext ()){
Entry = (Map. Entry) entries. next ();
Name = (String) entry. getKey ();
Object valueObj = entry. getValue ();
If (null = valueObj ){
Value = "";
} Else if (valueObj instanceof String []) {
String [] values = (String []) valueObj;
For (int I = 0; I <values. length; I ++ ){
Value = values [I] + ",";
}
Value = value. substring (0, value. length ()-1 );
} Else {
Value = valueObj. toString ();
}
ReturnMap. put (name, value );
}
Return returnMap;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.