表单接收数据的三种方式

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

 

你可能感兴趣的:(JSP,JSP表单接收)