首页 > 代码库 > JavaWeb表单数据的获取方式
JavaWeb表单数据的获取方式
表单页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="http://www.mamicode.com/"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> <center> <form action="<%=request.getContextPath() %>/Aservlet"method="post"> 姓名<input type="text" name="name" ><br> 性别:<input type="radio" name="sex" value="http://www.mamicode.com/男">男 <input type="radio" name="sex" value="http://www.mamicode.com/女">女 <br> 学历:<select name="education"><option value="http://www.mamicode.com/doctor">博士</option><option value="http://www.mamicode.com/master">硕士</option><option value="http://www.mamicode.com/bachelor">本科</option><option value="http://www.mamicode.com/junior">大专</option></select><br> 爱好:<input type="checkbox" name="habby" value="http://www.mamicode.com/read">读书 <input type="checkbox" name="habby" value="http://www.mamicode.com/travel">旅行<input type="checkbox" name="habby" value="http://www.mamicode.com/run">跑步<input type="checkbox" name="habby" value="http://www.mamicode.com/sing">唱歌<br> 工作经历:<textarea name="works" rows="10" cols="30"></textarea><br><input type="submit" value="http://www.mamicode.com/提交" style="width: 150px;height: 30px;"> </form> </center> </body></html>
Servlet
第一种方式实现(普通式)
/*第一种方式*/ writer.println("<hr>"+"<h2>第一种方式实现(普通式)</h2>"); String education2 = ""; String naemString = request.getParameter("name"); String sex = request.getParameter("sex"); String education = request.getParameter("education"); String[] habbys = request.getParameterValues("habby"); String works = request.getParameter("works"); switch (education) { case "doctor": education2 = "博士"; break; case "master": education2 = "硕士"; break; case "bachelor": education2 = "本科"; break; case "junior": education2 = "大专"; break; default: break; } writer.println("姓名:" + naemString + "<br>"); writer.print("<br>"); writer.println("性别:" + sex + "<br>"); writer.print("<br>"); writer.println("学历:" + education2 + "<br>"); writer.print("<br>"); writer.print("爱好:" + "<br>"); for (String habby : habbys) { writer.println(habby); } writer.print("<br>"); writer.print("<br>"); writer.println("工作经历:" + works + "<br>");
第二种方式实现(通过EntrySet遍历Map集合)
/*第二种方式*/ writer.println("<hr>"+"<h2>第二种方式实现(通过EntrySet遍历Map集合)</h2>"); Map<String, String[]> parameterMap = request.getParameterMap(); for (Entry<String, String[]> entry : parameterMap.entrySet()) { if (entry.getValue().length>1) { writer.print(entry.getKey()+": "+"<br>"); for (String string : entry.getValue()) { writer.print(string+" "+"<br>"); } }else { writer.print(entry.getKey()+":"+entry.getValue()[0]+" "+"<br>"); } }
第三种方式实现(通过JavaBean的内省机制)
/*第三种方式实现*/ writer.println("<hr>"+"<h2>第三种方式实现(通过JavaBean的内省机制)</h2>"); Map<String, String[]> parameterMap2 = request.getParameterMap(); UserBean userBean = new UserBean(); try { BeanUtils.populate(userBean, parameterMap2); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } writer.print("姓名"+" "+userBean.getName()+"<br>"); writer.print("性别"+" "+userBean.getSex()+"<br>"); writer.print("学历"+" "+userBean.getEducation()+"<br>"); if (userBean.getHabby().length>1) { writer.print("爱好"+"<br>"); for (String string : userBean.getHabby()) { writer.print(string+"、"); } } writer.print("<br>"); writer.print("工作经历"); writer.print("<br>"); writer.print(userBean.getWorks()); writer.print("<hr>"); writer.print(" <a href=http://www.mamicode.com/‘http://www.cnblogs.com/heerpeng/‘>更多方式"); }
JavaWeb表单数据的获取方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。