美文网首页
【Java】【web】获取表单数据

【Java】【web】获取表单数据

作者: JerichoPH | 来源:发表于2017-04-08 09:57 被阅读27次

    获取表单数据

    • getParameter(String name) 通过名称获取表单数据

    • getParameterValues(String name) 提取复选框的方法

    • getParameterNames() 获取表单所有名称

    • getParameterMap() 获取表单提交的所有值

    • getInputStream() 获取表单字节流

    • html部分

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="/demo" method="post">
        姓名:<input type="text" name="name" placeholder="姓名" value="测试">
        年龄:<input type="number" name="age" min="1" max="99" value="31">
        手机号:<input type="text" name="mobile" placeholder="手机号" value="13522178057">
        技能:
        <label><input type="checkbox" name="language" value="java">Java</label>
        <label><input type="checkbox" name="language" value="php">PHP</label>
        <label><input type="checkbox" name="language" value="python">Python</label>
        性别:
        <label><input type="radio" name="gender" value="1">男</label>
        <label><input type="radio" name="gender" value="0">女</label>
        <label><input type="submit" value="提交"></label>
    </form>
    </body>
    </html>
    
    • servlet部分
    public class Demo extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            Enumeration ids = request.getParameterNames();
            while (ids.hasMoreElements()) {
                String name = (String) ids.nextElement();
                System.out.print(name + ":");
                String[] values = request.getParameterValues(name);
                for (int i = 0; i < values.length; i++) {
                    System.out.println(values[i]);
                }
            }
        }
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            System.out.println("id:" + request.getParameter("id"));
        }
    }
    
    • 直接填充到实体类中
    @WebServlet(name = "Demo")
    public class Demo extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
        
            TestEntity t = new TestEntity();
        
            System.out.println("封装数据前" + t);
        
            Map<String, String[]> map = request.getParameterMap();
        
            for (Map.Entry<String, String[]> m : map.entrySet()) {
                String name = m.getKey();
                String[] values = m.getValue();
        
                try {
                    // 创建属性描述器
                    PropertyDescriptor pd;
                    pd = new PropertyDescriptor(name, TestEntity.class);
                    // 获得setter
                    Method setter = pd.getWriteMethod();
                    if (values.length == 1) {
                        setter.invoke(t, values[0]);
                    } else {
                        setter.invoke(t, (Object) values);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        
            }
        
            System.out.println("封装数据后:" + t);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        }
    }
    

    相关文章

      网友评论

          本文标题:【Java】【web】获取表单数据

          本文链接:https://www.haomeiwen.com/subject/knclattx.html