码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb表单数据的获取方式

时间:2016-09-28 19:14:46      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

表单页面

<%@ 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="<%=basePath%>"> <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="styles.css"> --> </head> <body> <center> <form action="<%=request.getContextPath() %>/Aservlet"method="post"> 姓名<input type="text" name="name" ><br> 性别:<input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女 <br> 学历:<select name="education"> <option value="doctor">博士</option> <option value="master">硕士</option> <option value="bachelor">本科</option> <option value="junior ">大专</option> </select><br> 爱好: <input type="checkbox" name="habby" value="read">读书 <input type="checkbox" name="habby" value="travel">旅行 <input type="checkbox" name="habby" value="run">跑步 <input type="checkbox" name="habby" value="sing">唱歌<br> 工作经历: <textarea name="works" rows="10" cols="30"> </textarea><br> <input type="submit" value="提交" 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.cnblogs.com/heerpeng/‘>更多方式</a>");
    }

 

 

 

  

JavaWeb表单数据的获取方式

标签:

原文地址:http://www.cnblogs.com/heerpeng/p/5917492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!