欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

表单接收数据的三种方式教程

程序员文章站 2022-03-30 11:47:13
1、普通方式 getparameter接收 request.setcharacterencoding("utf-8"); string...

1、普通方式 getparameter接收

      request.setcharacterencoding("utf-8");
   string username=request.getparameter("username");
   string pwd=request.getparameter("pwd");
   string gender=request.getparameter("gender");
   string[] hobbys=request.getparametervalues("hobby");
   string hobby=arrays.tostring(hobbys);
  
   string province=request.getparameter("province");
   string[] professors=request.getparametervalues("professor");
   string professor=arrays.tostring(professors);
   string introduct=request.getparameter("introduct");

2、枚举接收

给多项选择的name做特殊标记(如末尾加*)

       request.setcharacterencoding("utf-8");
       //request.getparameternames():返回客户端所有控件的name属性值组成的枚举器对象
       enumeration enu=request.getparameternames();
       while(enu.hasmoreelements())
       {
           string name=enu.nextelement();
           string value=null;
           if(name.endswith("*")){
           string[] values=request.getparametervalues(name);
           value=arrays.tostring(values);
           }
           else{
           value=request.getparameter(name); 
           }
           out.println(name+"="+value+"
");
       }

3、利用.getparametermap()接收

       request.setcharacterencoding("utf-8");
       map map= request.getparametermap();
       set> sets=map.entryset();
       for(entry entry:sets)
       {
        string name=entry.getkey();
        string[] value=entry.getvalue();
        out.println(name+"="+arrays.tostring(value)+"
");
       }