---
title: "Servlet获取表单数据"
author: "cakin24"
date: "2017-10-14T08:58:30Z"
tags: []
categories: ["编程语言"]
slug: "servlet"
draft: false
lastmod: "2017-10-14T08:58:30Z"
id: 2396268
url: "https://cakin24.iteye.com/blog/2396268"
---

一 用户实体类

 package entity;

import java.util.Date;

//用户实体类

public class Users {

 

        private String username; //用户名

        private String mypassword; //密码

        private String email; //电子邮箱

        private String gender; //性别

        private Date birthday; //出生日期

        private String\[] favorites; //爱好

        private String introduce; //自我介绍

        private boolean flag; //是否接受协议

        

        public Users()

        {

                

        }

        

        public String getUsername() {

                return username;

        }

        public void setUsername(String username) {

                this.username = username;

        }

        public String getMypassword() {

                return mypassword;

        }

        public void setMypassword(String mypassword) {

                this.mypassword = mypassword;

        }

        public String getEmail() {

                return email;

        }

        public void setEmail(String email) {

                this.email = email;

        }

        public String getGender() {

                return gender;

        }

        public void setGender(String gender) {

                this.gender = gender;

        }

        public Date getBirthday() {

                return birthday;

        }

        public void setBirthday(Date birthday) {

                this.birthday = birthday;

        }

        public String\[] getFavorites() {

                return favorites;

        }

        public void setFavorites(String\[] favorites) {

                this.favorites = favorites;

        }

        public String getIntroduce() {

                return introduce;

        }

        public void setIntroduce(String introduce) {

                this.introduce = introduce;

        }

 

        public boolean isFlag() {

                return flag;

        }

 

        public void setFlag(boolean flag) {

                this.flag = flag;

        }

 

}

二 Servlet类

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import entity.Users;

public class RegServlet extends HttpServlet {

    /\**

     * Constructor of the object.

     \*/

    public RegServlet() {

        super();

    }

    /\**

     * Destruction of the servlet. &lt;br&gt;

     \*/

    public void destroy() {

        super.destroy(); // Just puts "destroy" string in log

        // Put your code here

    }

    /\**

     * The doGet method of the servlet. &lt;br&gt;

     *

     * This method is called when a form has its tag value method equals to get.

     *

     * @param request the request send by the client to the server

     * @param response the response send by the server to the client

     * @throws ServletException if an error occurred

     * @throws IOException if an error occurred

     \*/

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        doPost(request,response);

    }

    /\**

     * The doPost method of the servlet. &lt;br&gt;

     *

     * This method is called when a form has its tag value method equals to post.

     *

     * @param request the request send by the client to the server

     * @param response the response send by the server to the client

     * @throws ServletException if an error occurred

     * @throws IOException if an error occurred

     \*/

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        

        Users u = new Users();

        String username,mypassword,gender,email,introduce,isAccept;

        Date birthday;

        String\[] favorites;

        

        

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try

        {

            username = request.getParameter("username");

            mypassword = request.getParameter("mypassword");

            gender = request.getParameter("gender");

            email = request.getParameter("email");

            introduce = request.getParameter("introduce");

            birthday = sdf.parse(request.getParameter("birthday"));

            if(request.getParameterValues("isAccpet")!=null)

            {

              isAccept = request.getParameter("isAccept");

            }

            else

            {

              isAccept = "false";

            }

            //用来获取多个复选按钮的值

            favorites = request.getParameterValues("favorite");

            u.setUsername(username);

            u.setMypassword(mypassword);

            u.setGender(gender);

            u.setEmail(email);

            u.setFavorites(favorites);

            u.setIntroduce(introduce);

            if(isAccept.equals("true"))

            {

                u.setFlag(true);

            }

            else

            {

                u.setFlag(false);

            }

            u.setBirthday(birthday);

            

            //把注册成功的用户对象保存在session中

            request.getSession().setAttribute("regUser", u);

            //跳转到注册成功页面

            request.getRequestDispatcher("../userinfo.jsp").forward(request,response);

        }

        catch(Exception ex)

        {

            ex.printStackTrace();

        }    

    }

    /\**

     * Initialization of the servlet. &lt;br&gt;

     *

     * @throws ServletException if an error occurs

     \*/

    public void init() throws ServletException {

        // Put your code here

    }

}

 

三 reg.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'reg.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

    &lt;style type="text/css"&gt;

         .label{

                  width: 20%     

         }

         .controler{

                  width: 80%     

         }

   &lt;/style&gt; 

   &lt;script type="text/javascript" src="js/Calendar3.js"&gt;&lt;/script&gt;

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;用户注册&lt;/h1&gt;

    &lt;hr&gt;

    &lt;form name="regForm" action="servlet/RegServlet" method="post" &gt;

                          &lt;table border="0" width="800" cellspacing="0" cellpadding="0"&gt;

                            &lt;tr&gt;

                                &lt;td class="lalel"&gt;用户名：&lt;/td&gt;

                                &lt;td class="controler"&gt;&lt;input type="text" name="username" /&gt;&lt;/td&gt;

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;密码：&lt;/td&gt;

                                &lt;td class="controler"&gt;&lt;input type="password" name="mypassword" &gt;&lt;/td&gt;

                                

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;确认密码：&lt;/td&gt;

                                &lt;td class="controler"&gt;&lt;input type="password" name="confirmpass" &gt;&lt;/td&gt;

                                

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;电子邮箱：&lt;/td&gt;

                                &lt;td class="controler"&gt;&lt;input type="text" name="email" &gt;&lt;/td&gt;

                                

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;性别：&lt;/td&gt;

                                &lt;td class="controler"&gt;&lt;input type="radio" name="gender" checked="checked" value="Male"&gt;男&lt;input type="radio" name="gender" value="Female"&gt;女&lt;/td&gt;

                                

                            &lt;/tr&gt;

                          

                            &lt;tr&gt;

                                &lt;td class="label"&gt;出生日期：&lt;/td&gt;

                                &lt;td class="controler"&gt;

                                  &lt;input name="birthday" type="text" id="control\_date" size="10"

                      maxlength="10" onclick="new Calendar().show(this);" readonly="readonly" /&gt;

                                &lt;/td&gt;

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;爱好：&lt;/td&gt;

                                &lt;td class="controler"&gt;

                                &lt;input type="checkbox" name="favorite" value="nba"&gt; NBA &nbsp;

                                  &lt;input type="checkbox" name="favorite" value="music"&gt; 音乐 &nbsp;

                                  &lt;input type="checkbox" name="favorite" value="movie"&gt; 电影 &nbsp;

                                  &lt;input type="checkbox" name="favorite" value="internet"&gt; 上网 &nbsp;

                                &lt;/td&gt;

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;自我介绍：&lt;/td&gt;

                                &lt;td class="controler"&gt;

                                        &lt;textarea name="introduce" rows="10" cols="40"&gt;&lt;/textarea&gt;

                                &lt;/td&gt;

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td class="label"&gt;接受协议：&lt;/td&gt;

                                &lt;td class="controler"&gt;

                                        &lt;input type="checkbox" name="isAccept" value="true"&gt;是否接受霸王条款

                                &lt;/td&gt;

                            &lt;/tr&gt;

                            &lt;tr&gt;

                                &lt;td colspan="2" align="center"&gt;

                                        &lt;input type="submit" value="注册"/&gt;&nbsp;&nbsp;

                                    &lt;input type="reset" value="取消"/&gt;&nbsp;&nbsp;

                                &lt;/td&gt;

                            &lt;/tr&gt;

                          &lt;/table&gt;

                        &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

四 userinfo.jsp

&lt;%@ page language="java" import="java.util.\*,java.text.\*" contentType="text/html; charset=utf-8"%&gt;

 

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'userinfo.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

    &lt;style type="text/css"&gt;

         .title{

                 width: 30%;     

                 background-color: #CCC;

                 font-weight: bold;

         }

         .content{

             width:70%;

             background-color: #CBCFE5;

         }

        

   &lt;/style&gt; 

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;用户信息&lt;/h1&gt;

    &lt;hr&gt;

    &lt;center&gt;

     &lt;jsp:useBean  id="regUser" class="entity.Users" scope="session"/&gt;  

     &lt;table width="600" cellpadding="0" cellspacing="0" border="1"&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;用户名：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;&lt;jsp:getProperty name="regUser" property="username"/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;密码：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;&lt;jsp:getProperty name="regUser" property="mypassword"/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;性别：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;&lt;jsp:getProperty name="regUser" property="gender"/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;E-mail：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;&lt;jsp:getProperty name="regUser" property="email"/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;出生日期：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;

            &lt;%

               SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

               String date = sdf.format(regUser.getBirthday());

              

            %&gt;

             &lt;%=date%&gt;

          &lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;爱好：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;

            &lt;%

               String\[] favorites = regUser.getFavorites();

               for(String f:favorites)

               {

            %&gt;

                &lt;%=f%&gt; &nbsp;&nbsp;

            &lt;%

               }

            %&gt;

          &lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;自我介绍：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;&lt;jsp:getProperty name="regUser" property="introduce"/&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

          &lt;td class="title"&gt;是否介绍协议：&lt;/td&gt;

          &lt;td class="content"&gt;&nbsp;&lt;jsp:getProperty name="regUser" property="flag"/&gt;&lt;/td&gt;

        &lt;/tr&gt;

     &lt;/table&gt;

    &lt;/center&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

五 测试效果

![](http://dl2.iteye.com/upload/attachment/0127/3675/3390c44e-e447-3b3b-9a56-1971b474d01c.png)