`
cakin24
  • 浏览: 1388413 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Servlet获取表单数据

    博客分类:
  • java
阅读更多
一 用户实体类
 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. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    /**
     * The doGet method of the servlet. <br>
     *
     * 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. <br>
     *
     * 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. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }
}
 
三 reg.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'reg.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">
        -->
    <style type="text/css">
         .label{
                  width: 20%     
         }
         .controler{
                  width: 80%     
         }
   </style> 
   <script type="text/javascript" src="js/Calendar3.js"></script>
  </head>
 
  <body>
    <h1>用户注册</h1>
    <hr>
    <form name="regForm" action="servlet/RegServlet" method="post" >
                          <table border="0" width="800" cellspacing="0" cellpadding="0">
                            <tr>
                                <td class="lalel">用户名:</td>
                                <td class="controler"><input type="text" name="username" /></td>
                            </tr>
                            <tr>
                                <td class="label">密码:</td>
                                <td class="controler"><input type="password" name="mypassword" ></td>
                                
                            </tr>
                            <tr>
                                <td class="label">确认密码:</td>
                                <td class="controler"><input type="password" name="confirmpass" ></td>
                                
                            </tr>
                            <tr>
                                <td class="label">电子邮箱:</td>
                                <td class="controler"><input type="text" name="email" ></td>
                                
                            </tr>
                            <tr>
                                <td class="label">性别:</td>
                                <td class="controler"><input type="radio" name="gender" checked="checked" value="Male"><input type="radio" name="gender" value="Female"></td>
                                
                            </tr>
                          
                            <tr>
                                <td class="label">出生日期:</td>
                                <td class="controler">
                                  <input name="birthday" type="text" id="control_date" size="10"
                      maxlength="10" onclick="new Calendar().show(this);" readonly="readonly" />
                                </td>
                            </tr>
                            <tr>
                                <td class="label">爱好:</td>
                                <td class="controler">
                                <input type="checkbox" name="favorite" value="nba"> NBA &nbsp;
                                  <input type="checkbox" name="favorite" value="music"> 音乐 &nbsp;
                                  <input type="checkbox" name="favorite" value="movie"> 电影 &nbsp;
                                  <input type="checkbox" name="favorite" value="internet"> 上网 &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <td class="label">自我介绍:</td>
                                <td class="controler">
                                        <textarea name="introduce" rows="10" cols="40"></textarea>
                                </td>
                            </tr>
                            <tr>
                                <td class="label">接受协议:</td>
                                <td class="controler">
                                        <input type="checkbox" name="isAccept" value="true">是否接受霸王条款
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                        <input type="submit" value="注册"/>&nbsp;&nbsp;
                                    <input type="reset" value="取消"/>&nbsp;&nbsp;
                                </td>
                            </tr>
                          </table>
                        </form>
  </body>
</html>
四 userinfo.jsp
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=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 'userinfo.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">
        -->
    <style type="text/css">
         .title{
                 width: 30%;     
                 background-color: #CCC;
                 font-weight: bold;
         }
         .content{
             width:70%;
             background-color: #CBCFE5;
         }
        
   </style> 
  </head>
 
  <body>
    <h1>用户信息</h1>
    <hr>
    <center>
     <jsp:useBean  id="regUser" class="entity.Users" scope="session"/>  
     <table width="600" cellpadding="0" cellspacing="0" border="1">
        <tr>
          <td class="title">用户名:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="username"/></td>
        </tr>
        <tr>
          <td class="title">密码:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="mypassword"/></td>
        </tr>
        <tr>
          <td class="title">性别:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="gender"/></td>
        </tr>
        <tr>
          <td class="title">E-mail:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="email"/></td>
        </tr>
        <tr>
          <td class="title">出生日期:</td>
          <td class="content">&nbsp;
            <%
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
               String date = sdf.format(regUser.getBirthday());
              
            %>
             <%=date%>
          </td>
        </tr>
        <tr>
          <td class="title">爱好:</td>
          <td class="content">&nbsp;
            <%
               String[] favorites = regUser.getFavorites();
               for(String f:favorites)
               {
            %>
                <%=f%> &nbsp;&nbsp;
            <%
               }
            %>
          </td>
        </tr>
        <tr>
          <td class="title">自我介绍:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="introduce"/></td>
        </tr>
        <tr>
          <td class="title">是否介绍协议:</td>
          <td class="content">&nbsp;<jsp:getProperty name="regUser" property="flag"/></td>
        </tr>
     </table>
    </center>
  </body>
</html>
 
五 测试效果

 
  • 大小: 52.4 KB
1
0
分享到:
评论

相关推荐

    十分全的servlet获取表单数据

    在这个主题“十分全的servlet获取表单数据”中,我们将深入探讨如何利用Servlet来处理客户端提交的表单信息,这包括各种输入控件如文本框、密码框、选择框、单选框、复选框以及文本域的数据获取。 1. **表单基础** ...

    servlet获取表单数据详解(十分全)

    本文将深入探讨如何使用Servlet来获取各种类型的表单数据。 首先,对于普通的输入框、密码框、隐藏字段等,我们可以使用`HttpServletRequest`对象的`getParameter()`方法。例如,如果JSP页面中有如下表单元素: ``...

    servlet接收form表单提交的数据

    // 获取表单数据 String username = request.getParameter("username"); String password = request.getParameter("password"); // 构建响应内容 StringBuilder html = new StringBuilder(); html.append(...

    Servlet表单Get和Post方式读取

    然后在Servlet中处理这个表单数据: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request....

    java Servlet对表单的处理过程

    在`doGet()`或`doPost()`方法中,我们可以通过`HttpServletRequest`对象获取表单数据。对于GET请求,数据在请求URL中,可以通过`getParameter()`方法获取;对于POST请求,数据在请求体中,同样使用`getParameter()`...

    _探讨Ajax获取表单值向Servlet传递的设计方案.doc

    本文探讨了 Ajax 获取表单值向 Servlet 传递的设计方案,包括了 XMLHttpRequest 对象的获取、GET 和 POST 方式的异步发送数据、回调函数的使用等内容,为开发者提供了一个实用的解决方案。 知识点: 1. Ajax 技术...

    Servlet中对获取网页表单数据自动给JavaBean赋值的BeanUtil工具类

    总的来说,BeanUtil工具类是Java Web开发中的实用工具,它简化了Servlet处理表单数据和JavaBean之间数据转换的过程,让代码更简洁、更易于维护。在学习和使用BeanUtil时,还需要了解其潜在的类型转换问题和安全性...

    servlet简单示例

    当用户提交表单时,Servlet获取表单数据并进行相应的数据库操作。注意,为了连接MySQL数据库,我们需要导入JDBC驱动,配置数据源,并编写SQL语句来执行增、删、改、查操作。 例如,增加用户可能的SQL语句如下: ``...

    javaIDEA新手研究的ajax访问servlet,html获取后台数据

    本项目是针对初学者设计的,主要涉及的知识点是利用Ajax技术与Servlet进行交互,从而实现HTML页面动态获取后台数据。我们将详细探讨这些关键概念和技术。 首先,Ajax(Asynchronous JavaScript and XML)是一种在不...

    获取网页表单数据并显示

    对于`POST`请求,我们在`servlet`中重写`doPost()`方法来获取表单数据: ```java import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletName ...

    Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例

    在Servlet中,我们可以使用request.getParameter()方法来获取表单提交的数据。这种方法简单易用,但是在处理表单数据时需要注意参数的编码问题。 方式二:使用 request.getParameterValues() 方法 如果表单中有多...

    Javaweb获取表单数据的多种方式

    在Java Web开发中,获取表单数据是常见的任务,它涉及到客户端与服务器之间的交互。本文将详细介绍在Javaweb环境中获取表单数据的多种方式,以帮助开发者更好地理解和处理用户提交的信息。 首先,最常用的方式是...

    Struts2表单数据获取项目

    在这个名为"Struts2表单数据获取项目"的资源中,我们将深入探讨如何使用Struts2框架来处理表单提交的数据。 首先,我们来看看核心组件`Struts.xml`。这是Struts2应用的主要配置文件,用于定义动作(Actions)、结果...

    10级javaEE实验报告

    实验要求学生能够编写、配置和运用Servlet,以及了解Servlet获取表单数据、读取Session和Cookie数据的技术。 实验内容分为以下几个部分: 1. `register.jsp`文件:这是一个简单的HTML表单,用户可以输入个人信息,...

    ExtJS+Servlet表单验证和注册

    **ExtJS简介** ExtJS是一款基于...总结来说,这个小例子展示了如何结合ExtJS的前端交互能力和Servlet的后端服务处理能力,构建一个完整的用户注册流程,包括前端的美观界面、表单验证以及后端的数据处理和存储。

    jsp网络编程从基础到实践

    实例63 用servlet获取表单数据 实例64 用servlet读写文件数据 实例65 用servlet访问数据库 实例66 一个简单的struts应用实例——用户登录 第10章 JSP中的JavaMail开发 实例67 发送文本格式的邮件 实例68 发送 ...

    JSP网络编程从基础到实践 实例代码

    实例63 用servlet获取表单数据 实例64 用servlet读写文件数据 实例65 用servlet访问数据库 实例66 一个简单的struts应用实例——用户登录 第10章 JSP中的JavaMail开发 实例67 发送文本格式的邮件 实例68 发送 ...

    JSP网络编程从基础到实践的实例代码

    实例63 用servlet获取表单数据 实例64 用servlet读写文件数据 实例65 用servlet访问数据库 实例66 一个简单的struts应用实例——用户登录 第10章 JSP中的JavaMail开发 实例67 发送文本格式的邮件 实例68 发送 ...

    Servlet及Ajax数据交互

    Servlet负责处理服务器端的业务逻辑,而Ajax则负责在客户端与服务器之间异步通信,确保页面无需完全刷新即可获取和更新数据。这种技术广泛应用于动态网页、表单提交、实时数据更新等多种场景。在实际开发中,开发者...

    jsp,servlet普通表单+文件上传

    在`FileUploadServlet`类中,重写`doPost`方法,使用`HttpServletRequest`对象获取文件数据。可以使用Apache Commons FileUpload库或Java内置的`Part`接口来处理文件上传。例如: ```java import org.apache....

Global site tag (gtag.js) - Google Analytics