1.我们创建用户信息注册页面:
<%@ 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 '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>
<h1>用户注册页面</h1>
<hr>
<form name="regForm" action="servlet/regServlet" method="post">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="mypassword"/></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="confirmpass"/></td>
</tr>
<tr>
<td>电子邮箱:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="gender" value="Male"/>男
<input type="radio" name="gender" value="Female"/>女
</td>
</tr>
<tr>
<td>出生日期:</td>
<td>
<input name="birthday" type="text" id="control_date" size="10"
maxlength="10" οnclick="new Calendar().show(this)" readonly="readonly" />
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="favorite" value="nbz">NBA
<input type="checkbox" name="favorite" value="music">音乐
<input type="checkbox" name="favorite" value="movie">电影
<input type="checkbox" name="favorite" value="internet">上网
</td>
</tr>
<tr>
<td>自我介绍:</td>
<td>
<textarea name="introduce" rows="10" cols="40"></textarea>
</td>
</tr>
<tr>
<td>接受协议:</td>
<td><input type="checkbox" name="isAccept" value="true">是否接受霸王条款</td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="submit" value="取消" /></td>
</tr>
</table>
</form>
</body>
</html>
2.创建用户对象实体类
package entity;
import java.util.Date;
//用户实体类
public class User {
private String username; //用户名
private String password; //密码
private String email; //邮箱
private String gender; //性别
private Date birthday; //出生日期
private String[] favorites; //爱好
private String introduce; //自我介绍
private boolean isAccept; //是否接受协议
public User(){
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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 isAccept() {
return isAccept;
}
public void setAccept(boolean isAccept) {
this.isAccept = isAccept;
}
}
3.创建Servlet类来接收页面页面提交到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.User;
public class regServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
User u = new User();
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("2015-09-09");
//用来获取多个复选按钮的值
favorites = request.getParameterValues("favorite");
isAccept = request.getParameter("isAccept");
u.setUsername(username);
u.setPassword(mypassword);
u.setGender(gender);
u.setEmail(email);
u.setIntroduce(introduce);
u.setBirthday(birthday);
u.setFavorites(favorites);
u.setAccept(isAccept.equals("true")?true:false);
//把注册成功的用户对象保存在Session中,
<strong>request.getSession().setAttribute("regUser",u);</strong>
//最后跳转到注册成功页面
request.getRequestDispatcher("../userinfo.jsp").forward(request, response);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
4.Servlet数据接受完毕后我们需要跳转到用户信息展示页面,展示用户注册信息。
<%@page import="java.text.SimpleDateFormat"%>
<%@ 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 '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>
<h1>用户注册页面</h1>
<hr>
<center>
<jsp:useBean scope="session" id="regUser" class="entity.User"/>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>用户名:</td>
<td><jsp:getProperty name="regUser" property="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><jsp:getProperty name="regUser" property="password" /></td>
</tr>
<tr>
<td>电子邮箱:</td>
<td><jsp:getProperty name="regUser" property="email" /></td>l
</tr>
<tr>
<td>性别:</td>
<td>
<jsp:getProperty name="regUser" property="gender" />
</td>
</tr>
<tr>
<td>出生日期:</td>
<td>
<% SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String date = sdf.format(regUser.getBirthday());
%>
<%=date %>
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<%
String[] favorites = regUser.getFavorites();
for(String f:favorites){
%>
<%=f %>
<%} %>
</td>
</tr>
<tr>
<td>自我介绍:</td>
<td>
<jsp:getProperty name="regUser" property="introduce" />
</td>
</tr>
</table>
</center>
</body>
</html>