Javaweb获取表单数据的多种方式
Javaweb获取表单数据的几种方式
一、通过键值对的形式获取表单数据
getParameter(Stringname):通过key,返回一个value。
getParameterValues(Stringname):通过key返回一个string数组(多个值)
getParameterNames():返回form表单中的所有key值。
下面介绍通过键值对获取form表单数据的数据的方法:
@WebServlet({"/FormServlet","/form"}) publicclassFormServletextendsHttpServlet{ privatestaticfinallongserialVersionUID=1L; publicFormServlet(){ super(); } protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ response.setContentType("text/json;charset=utf-8"); PrintWriterout=response.getWriter(); Enumeration<String>paramNames=request.getParameterNames(); while(paramNames.hasMoreElements()){ Stringname=paramNames.nextElement(); String[]values=request.getParameterValues(name); if(values!=null&&values.length>0){ StringBuilderbuilder=newStringBuilder(); for(inti=0;i<values.length;i++){ builder.append(values[i]+""); } out.println(name+":"+builder.toString()); } } } protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doGet(request,response); } }
form表单:
<!DOCTYPEhtml> <html> <head> <metacharset="UTF-8"> <title>Inserttitlehere</title> </head> <body> <formaction="/TomcatDemo/form"method="post"> 用户名<inputtype="text"name="username"/><br/> 密码<inputtype="password"name="password"/><br/> 性别<inputtype="radio"name="sex"value="male"checked="checked"/>男 <inputtype="radio"name="sex"value="female"/>女<br/> 爱好<inputtype="checkbox"name="hobby"value="basketball"/>篮球 <inputtype="checkbox"name="hobby"value="football"/>足球 <inputtype="checkbox"name="hobby"value="game"/>游戏 <inputtype="checkbox"name="hobby"value="media"/>电影<br/> 城市<selectname="city"> <optionvalue="bj">北京</option> <optionvalue="sh">上海</option> <optionvalue="sz">深圳</option> <optionvalue="hz">杭州</option> </select><br/> <inputtype="submit"value="注册"/> <inputtype="submit"value="登入"/><br/> </form> </body> </html>
二、通过Map的形式获取表单数据
getParameterMap():获取form表单的数据,以map的格式封装起来
示例:
@WebServlet({"/FormServlet","/form"}) publicclassFormServletextendsHttpServlet{ privatestaticfinallongserialVersionUID=1L; publicFormServlet(){ super(); } protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ response.setContentType("text/json;charset=utf-8"); PrintWriterout=response.getWriter(); Useruser=newUser(); out.println("获取表单数据之前:"+user.toString()); Map<String,String[]>map=request.getParameterMap(); for(Map.Entry<String,String[]>m:map.entrySet()){ Stringname=m.getKey(); String[]values=m.getValue(); //属性描述器:表示JavaBean类通过存储器导出一个属性 PropertyDescriptorpd=null; try{ pd=newPropertyDescriptor(name,User.class); }catch(IntrospectionExceptione){ e.printStackTrace(); } if(values!=null&&pd!=null){ Methodsetter=pd.getWriteMethod(); try{ if(values.length==1){ setter.invoke(user,values[0]); }else{ setter.invoke(user,(Object)values); } }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } } } out.println("获取表单数据之后:"+user.toString()); } protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doGet(request,response); } }
PropertyDescriptor的使用,点击查看
User类
publicclassUser{ privateStringusername;//属性名称需要和表单数据中的name值保持一致 privateStringpassword; privateStringsex; privateString[]hobby; privateStringcity; ... //setget方法省略 }
三、通过第三方jar包获取封装表单数据
使用第三方jar包:commons-beanutils-1.8.3.jar
@WebServlet({"/FormServlet","/form"}) publicclassFormServletextendsHttpServlet{ privatestaticfinallongserialVersionUID=1L; publicFormServlet(){ super(); } protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ response.setContentType("text/json;charset=utf-8"); PrintWriterout=response.getWriter(); Useruser=newUser(); out.println("获取表单数据之前:"+user.toString()); try{ //通过第三方jar包处理 BeanUtils.populate(user,request.getParameterMap()); }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } out.println("获取表单数据之后:"+user.toString()); } protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doGet(request,response); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。