HTML
<body> <form action="/day06/RequestDemo1"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="password"><br> 验证码:<input type="text" name="checkcode"> <img src="/day06/ResponseDemo4" onclick="this.src=this.src+'?'+new Date().getTime()" alt="换一张" style="cursor:pointer"><br> <input type="submit" value="提交"> </form> </body>
有一个表单,下面是servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取表单提交的所有名称和值 Enumeration e = request.getParameterNames(); while(e.hasMoreElements()){ String name = (String) e.nextElement(); String value = request.getParameter(name); System.out.println(name+": "+value); } }
提交在控制台得到数据
可以用工具传递数据,注意导包
System.out.println("---------方法二-----------"); Map<String, String[]> map = request.getParameterMap(); User user = new User(); try{ BeanUtils.populate(user, map); //<span style="white-space:pre"> </span> BeanUtils.copyProperties(user, bean); bean的拷贝 }catch(Exception e1){ e1.printStackTrace(); } System.out.println(user);