实现步骤
1.创建UserBean类
package cn.itcast.chapter08.model2.domain;
public class UserBean {
private String name;
private String password;
private String email;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
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;
}
}
2.创建RegisterFormBean类
package cn.itcast.chapter08.model2.domain;
import java.util.HashMap;
import java.util.Map;
public class RegisterFormBean {
private String name;
private String password;
private String password2;
private String email;
// 定义成员变量errors,用于封装表单验证时的错误信息
private Map<String, String> errors = new HashMap<String, String>();
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword2(){
return password2;
}
public void setPassword2(String password2){
this.password2=password2;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email=email;
}
public boolean validate(){
boolean flag = true;
if(name == null || name.trim().equals("")){
errors.put("name", "请输入姓名.");
flag = false;
}
if (password == null || password.trim().equals("")){
errors.put("password", "请输入密码.");
flag=false;
} else if (password.length() > 12 || password.length() < 6) {
errors.put("password", "请输入6-12个字符.");
flag=false;
}
if (password != null && !password.equals(password2)) {
errors.put("password2", "两次输入的密码不匹配.");
flag=false;
}
// 对email格式的校验采用了正则表达式
if (email == null || email.trim().equals("")) {
errors.put("email", "请输入邮箱.");
flag=false;
} else if (!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+")) {
errors.put("email", "邮箱格式错误.");
flag=false;
}
return flag;
}
// 向Map集合errors中添加错误信息
public void setErrorMsg(String err, String errMsg){
if ((err != null) && (errMsg != null)) {
errors.put(err, errMsg);
}
}
// 获取errors集合
public Map<String, String> getErrors(){
return errors;
}
}
3.创建DBUtil类
package cn.itcast.chapter08.model2.util;
import cn.itcast.chapter08.model2.domain.UserBean;
import java.util.HashMap;
public class DBUtil {
private static DBUtil instance = new DBUtil();
// 定义users集合,用于模拟数据库
private HashMap<String, UserBean> users = new HashMap<String, UserBean>();
private DBUtil() {
UserBean user1 = new UserBean();
user1.setName("Jack");
user1.setPassword("12345678");
user1.setEmail("jack@it315.org");
users.put("Jack", user1);
UserBean user2 = new UserBean();
user2.setName("Rose");
user2.setPassword("abcdefg");
user2.setEmail("rose@it315.org");
users.put("Rose", user2);
}
public static DBUtil getInstance(){
return instance;
}
public UserBean getUser(String userName){
UserBean user = (UserBean) users.get(userName);
return user;
}
// 向数据库中插入数据
public boolean insertUser(UserBean user) {
if(user == null) {
return false;
}
String userName = user.getName();
if(users.get(userName) != null) {
return false;
}
users.put(userName, user);
return true;
}
}
4.创建ControllerServlet类
package cn.itcast.chapter08.model2.web;
import cn.itcast.chapter08.model2.domain.RegisterFormBean;
import cn.itcast.chapter08.model2.domain.UserBean;
import cn.itcast.chapter08.model2.util.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "ControllerServlet", urlPatterns = "/ControllerServlet")
public class ControllerServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Content-type", "text/html;charset=GBK");
response.setCharacterEncoding("GBK");
// 获取用户注册时表单提交的参数信息
String name = request.getParameter("name");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");
String email = request.getParameter("email");
// 将获取的参数封装到注册表单相关的RegisterFormBean类中
RegisterFormBean formBean = new RegisterFormBean();
formBean.setName(name);
formBean.setPassword(password);
formBean.setPassword2(password2);
formBean.setEmail(email);
// 验证参数填写是否符合要求
if(!formBean.validate()) {
request.setAttribute("formBean", formBean);
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
// 如果参数填写符合要求,则将数据封装到UserBean类中
UserBean userBean = new UserBean();
userBean.setName(name);
userBean.setPassword(password);
userBean.setEmail(email);
// 调用DBUtil类中的insertUser()方法执行添加操作
boolean b = DBUtil.getInstance().insertUser(userBean);
// 如果返回false,表示注册的用户已存在
if(!b){
request.setAttribute("DBMes", "你注册的用户已存在");
request.setAttribute("formBean", formBean);
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
response.getWriter().print("恭喜你注册成功,3秒钟自动跳转");
// 将成功注册的用户信息添加到Session中
request.getSession().setAttribute("userBean", userBean);
// 注册成功后,跳转页面
response.setHeader("refresh", "3;url=loginSuccess.jsp");
}
}
5.创建regiser的jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="GBK" %>
<html>
<head>
<title>用户注册</title>
<style type="text/css">
h3 {
margin-left: 100px;
}
#outer {
width: 750px;
}
span {
color: #ff0000;
}
div {
height: 20px;
margin-bottom: 10px;
}
.ch {
width: 80px;
text-align: right;
float: left;
}
.ip {
width: 500px;
float: left;
}
.ip>input {
margin-right: 20px;
}
#bt {
margin-left: 50px;
}
#bt>input {
margin-right: 30px;
}
</style>
</head>
<body>
<form action="/test_war_exploded/ControllerServlet" method="post">
<h3>用户注册</h3>
<div id="outer">
<div>
<div class="ch">姓名:</div>
<div class="ip">
<input type="text" name="name" value="${formBean.name}" />
<span>${formBean.errors.name}${DBMes}</span>
</div>
</div>
<div>
<div class="ch">密码:</div>
<div class="ip">
<input type="text" name="password">
<span>${formBean.errors.password}</span>
</div>
</div>
<div>
<div class="ch">确认密码:</div>
<div class="ip">
<input type="text" name="password2">
<span>${formBean.errors.password2}</span>
</div>
</div>
<div>
<div class="ch">邮箱:</div>
<div class="ip">
<input type="text" name="email" value="${formBean.email}">
<span>${formBean.errors.email}</span>
</div>
</div>
<div id="bt">
<input type="reset" value="重置">
<input type="submit" value="注册">
</div>
</div>
</form>
</body>
</html>
6.创建loginSuccess的jsp文件
<%@ page language="java" pageEncoding="GBK" import="cn.itcast.chapter08.model2.domain.UserBean" %>
<html>
<head>
<title>登录成功</title>
<style type="text/css">
#main {
width: 500px;
height: auto;
}
#main div {
width: 200px;
height: auto;
}
ul {
padding-top: 1px;
padding-left: 1px;
list-style: none;
}
</style>
</head>
<body>
<%
if (session.getAttribute("userBean") == null) {
%>
<jsp:forward page="register.jsp" />
<%
return;
}
%>
<div id="main">
<div id="welcome">恭喜你,登录成功</div>
<hr/>
<div>您的信息</div>
<div>
<ul>
<li>您的姓名:${userBean.name }</li>
<li>您的邮箱:${userBean.email}</li>
</ul>
</div>
</div>
</body>
</html>
7.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>cn.itcast.chapter08.model2.web.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>
</web-app>