Request.getparametermap () usage notes in Java

Source: Internet
Author: User

Request.getparametermap () usage notes in Java

One

According to the Java Specification: Request.getparametermap () returns the value of a map type that records the mapping of request parameters and request parameter values from the front-end (such as JSP pages) submitted requests. There's something special about this return value--Read only. Not as normal map type data can be modified. This is because of the limitations that the server has made in order to achieve certain security specifications. such as Weblogic,tomcat,resin,jboss and other servers have implemented this specification.

If it is really necessary to make changes after this value is made, create a new map object, copy the return value to the new map object, and replace the return value with the new map object.

Map Readonlymap = Request.getparametermap ();
Map Writeablemap = new HashMap ();
Writeablemap.putall (READONLYMAP);
Writeablemap.remove () or put ()
... Use Writeablemap in subsequent program code

Two

The return value for Request.getparametermap () should be in map<string,string[]> form when using generics, because sometimes a component like a checkbox will have a name to deal with value. So the key value pair in the map is the implementation of <String-->String[]>.

For example, it is easy to get the parameters submitted by the JSP page on the server side, but by Request.getparametermap () you can turn the parameters and values in the request into a map.

The following are the resulting parameters and values are printed, the resulting map structure: map (key,value[]), namely: Key is string, value is a string array.

For example: The parameters in Request t1=1&t1=2&t2=3 the map structure:

key=t1;value[0]=1,value[1]=2

Key=t2;value[0]=3

If the direct use of Map.get ("T1"), the resulting will be: Ljava.lang.String; Value is only an array form, which prevents the parameter names from having the same situation.

Map map = Request.getparametermap ();  
Java.util.Enumeration  enum=this.getrequest (). Getparameternames ();  
       while (Enum.hasmoreelements ()) {  
                  string  paramname= (String) enum.nextelement ();                      
                  String[]  values=request.getparametervalues (paramname);  
                  for (int  i=0;i<values.length;i++) {  
                  System.out.println ("[+i+"]   "+paramname+"  "+values[i]); 
                  } 
       }

Third, when the number of parameters are not fixed and the parameter name does not duplicate, the method of taking values:

Map map = new HashMap ();

Gets the name of the parameter for the enumeration type, and the name of the parameter, if repeated, can only get the first

Enumeration enum =this.getrequest (). Getparameternames ();
 while (Enum.hasmoreelements ()) {
  String paramname = (string) enum.nextelement ();
 
  String paramvalue = This.getrequest (). GetParameter (paramname);
   Map
  map.put (paramname, paramvalue) corresponding to the formation of the key value;
  }



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.