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="<%=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>
 
 

Servletcss

第一種方式實現(普通式)html

/*第一種方式*/
    
        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集合)
java

/*第二種方式*/
        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的內省機制)jsp

 

        /*第三種方式實現*/
        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>");
    }
相關文章
相關標籤/搜索