第四章:Servlet中获取表单form参数信息

一。方法

    1.通过getParameter()获得参数,返回一个string类型

    2.通过getParameterValues()获得参数,返回string[]数组

    3.通过getParameterNames()获得参数,返回Enumeration类型的值

    4.通过getParameterMap()获得参数,返回map(string, string[])

二。示例

public class TestServlet extends HttpServlet {
       private static final long serialVersionUID = 1711689663622072980L;
       protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException       {
              resp.setContentType("text/html;charset=UTF-8");
              PrintWriter out = resp.getWriter();
              out.println("<html>");
              out.println("<head>");
              out.println("<title>HttpSevlet获得参数的方法</title>");
              out.println("</head>");
              out.println("<body>");
              //设置接收参数所用的编码
              req.setCharacterEncoding("UTF-8");
//通过getParameter()获得的参数
              out.println("getParameter()获得的参数<br>");
              String value = null;
              //获得name=“txt”表单控件的值
              value=req.getParameter("text");
              out.println("输入框内输入的值:"+value);
              out.print("<br>");
              //获得name="chk"的表单控件的值
              value=req.getParameter("checkbox");
              out.println("多选框选中的值:"+value);
              out.print("<br>");
              //获得name="sel"的表单控件的值
              value=req.getParameter("select");
              out.println("下拉框选中的值:"+value);
              out.print("<br>");
    
//通过getParameterValues()获得的参数
              out.println("getParameterValues()获得的参数<br>");
              String []paramValue = null;
              //获得name=“txt”表单控件的值
              paramValue=req.getParameterValues("text");
              if(paramValue!=null){
                     out.print("输入框内输入的值:");
                     for(int i=0;i<paramValue.length;i++){
                            out.print(paramValue[i]);
                            out.print("&nbsp;&nbsp;");
                     }
              }
              out.print("<br>");
              //获得name="chk"的表单控件的值
              paramValue=req.getParameterValues("checkbox");
              if(paramValue!=null){
                     out.print("多选框选中的值:");
                     for(int i=0;i<paramValue.length;i++){
                            out.print(paramValue[i]);
                            out.print("&nbsp;&nbsp;");
                     }
              }
              out.print("<br>");
              //获得name="sel"的表单控件的值
              paramValue=req.getParameterValues("select");
              if(paramValue!=null){
                     out.println("下拉框选中的值:");
                     for(int i=0;i<paramValue.length;i++){
                            out.println(paramValue[i]);
                            out.println("&nbsp;&nbsp;");
                     }
              }
              out.print("<br>");  
             
//通过getParameterMap()获得的参数
              out.println("getParameterMap()获得的参数<br>");
              //获得参数的Map
              Map paramMap = req.getParameterMap();
              Object o =null;
              String []val = null;
              //循环Map的key
              for(Iterator it = paramMap.keySet().iterator();it.hasNext();){
                     o=it.next();
                     out.print(o);
                     out.print(":");
                     //获得key对应的value
                     val=(String []) paramMap.get(o);
                     if(val != null){
                            for(int j=0;j<val.length;j++){
                                   out.println(val[j]);
                                   out.println("&nbsp;&nbsp;");
                            }
                     }
              }
              out.print("<br>");   
             
//通过getParameterNames()获得的参数
              out.println("getParameterNames()获得的参数<br>");
              //获得参数名称枚举
              Enumeration en = req.getParameterNames();
              for(;en.hasMoreElements();){
                     o=en.nextElement();
                     out.println(o);
                     out.println("&nbsp;&nbsp;");
              }
              out.print("<br>");
        
              out.println("</head>");
              out.println("</html>"); 
       }
//通过getParameterMap()获得的参数
        。。。。。。
       //设置浏览器端解析文件的方式为html,字符码
        resp.setContentType("text/html;charset=gb2312");
        pw = resp.getWriter();//获得输出流
        Map<String, String[]> map = req.getParameterMap();
        //获得迭代器对象
	Iterator<Entry<String, String[]>> entries = map.entrySet().iterator();
	while(entries.hasNext()){
	//得到下一个元素
	     Map.Entry entry = (java.util.Map.Entry) entries.next();
	     String key = (String) entry.getKey();//得到键
	     String[] values = (String[])entry.getValue();//得到值
	     if(values.length==1){//判断键对应的值的个数是1
	    	   。。。。。。
	     }else{      //值的个数是多个
	    	   for (int i = 0; i < values.length; i++) {
	    	   	String value = values[i];   
                        。。。。。。
	    	   }
	      }
	}
}


你可能感兴趣的:(第四章:Servlet中获取表单form参数信息)