下面是步骤,有需要的可以参考源码:
1。创建项目,编写bean
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。创建类封装注册表单信息,并且验证表单信息
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,模拟数据库
import java.util.HashMap;
import cn.itcast.chapter11.model2.domain.UserBean;
public class DBUtil {
private static DBUtil instance = new DBUtil(); //
// 定义users集合,用于模拟数据库
private HashMap<String,UserBean> users = new HashMap<String,UserBean>();
private DBUtil() {
// 向数据库(users)中存入两条数据
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;
}
// 获取数据库(users)中的数据
public UserBean getUser(String userName) {
UserBean user = (UserBean) users.get(userName);
return user;
}
// 向数据库(users)插入数据
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。创建servlet,用于视图控制。
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.chapter11.model2.domain.RegisterFormBean;
import cn.itcast.chapter11.model2.domain.UserBean;
import cn.itcast.chapter11.model2.util.DBUtil;
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);
// 验证参数填写是否符合要求,如果不符合,转发到register.jsp重新填写
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类型的标志
boolean b = DBUtil.getInstance().insertUser(userBean);
// 如果返回为false,表示注册的用户已存在,则重定向到register.jsp重新填写
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);
// 注册成功后,3秒跳转到登录成功页面loginSuccess.jsp
response.setHeader("refresh", "3;url=loginSuccess.jsp");
}
}
5。常见注册页面,请求提交给servlet
<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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="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="password" name="password" />
<span>${formBean.errors.password}</span>
</div>
</div>
<div>
<div class="ch">确认密码:</div>
<div class="ip">
<input type="password" 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。创建注册成功页面
<%@ page language="java" pageEncoding="GBK" import="cn.itcast.chapter11.model2.domain.UserBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>login successfully</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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>用户注册MVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>register.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welc**加粗样式**ome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ControllerServlet</display-name>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>cn.itcast.chapter11.model2.web.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>
</web-app>
8。运行: