首页 > 代码库 > request对象封装表单数据

request对象封装表单数据

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        test1(request);
//        test2(request);
        test3(request);
    }
//获取单个控件的值
    public void test1(HttpServletRequest request){
        String name = request.getParameter("username");
        System.out.println("name=" + name);
    }
    //获取同名控件的值
    public void test2(HttpServletRequest request){
        String[] age = request.getParameterValues("age");
        for (String string : age) {
            System.out.println("string=" + string);
        }
    }
    //获取全部控件
    public void test3(HttpServletRequest request){
        //获取所有控件的名字
        System.out.println("--------request--------" + request.toString());
        Enumeration<String> enu = request.getParameterNames();
        while(enu.hasMoreElements()){
            String name = enu.nextElement();
            //可能有多个重复
            String[] values = request.getParameterValues(name);
            for (String value : values) {
                System.out.println(name + "---" + value);
            }
        }
    }
//将表单中的所有值封装到JavaBean中

 

request对象封装表单数据