包中定义UserBean类,用于封装用户信息

package com.example.javaweb16;

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;
    }

}

包中定义RegisterFormBean类,用于封装注册表单信息

package com.example.javaweb16;


import java.util.HashMap;
import java.util.Map;


public class RegisterFormBean {
    private String name;
    private String password;
    private String password2;
    private String email;
    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;
    }
}

在包中定义DBUtil类

package com.example.javaweb16;


import java.util.HashMap;

import com.example.javaweb16.UserBean;

public class DBUtil {
    private static DBUtil instance = new DBUtil();
    private HashMap<String, UserBean> users = new HashMap<String, UserBean>();
    private DBUtil() {
        UserBean user1 = new UserBean();
        user1.setName("admin");
        user1.setPassword("12345678");
        user1.setEmail("qwe@qq.com");
        users.put("admin", user1);
        UserBean user2 = new UserBean();
        user2.setName("admin");
        user2.setPassword("abcdegfg");
        user2.setEmail("admin@qq.com");
    }
    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;
    }
}

在包中定义ControllerServlet类,使用该类来处理用户请求

package com.example.javaweb16;


import java.io.IOException;

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 com.example.javaweb16.RegisterFormBean;
import com.example.javaweb16.UserBean;
import com.example.javaweb16.DBUtil;
@WebServlet("/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 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;
        }
        System.out.println("!!!");
        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPassword(password);
        userBean.setEmail(email);

        boolean b = DBUtil.getInstance().insertUser(userBean);
        if (!b) {
            request.setAttribute("DBMes", "你注册的用户已存在");
            request.setAttribute("formBean", formBean);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            return;
        }
        response.getWriter().print("注册成功,3秒后自动跳转");

        request.getSession().setAttribute("userBean", userBean);

        response.setHeader("refresh", "3;url = loginSuccess.jsp");
    }
}

创建register.jsp文件,该文件是用户注册的表单页面,用于接收用户的注册信息。

<%--
  Created by IntelliJ IDEA.
  User: chenci
  Date: 2021/12/19
  Time: 9:35 下午
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>

编写loginSuccess.jsp文件

<%@ page language="java" pageEncoding="GBK" import="com.example.javaweb16.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>

<div id="main">
  <div id="welcome">恭喜你,登录成功</div>
  <hr />
  <div>您的信息</div>
  <div>
    <ul>
      <li>您的姓名: ${userBean.name }</li>
      <li>您的邮箱:${userBean.email}</li>
      <li>您的邮箱:${userBean.password}</li>
    </ul>
  </div>
</div>
</body>
</html>

测试
BB0C3F75-18FA-4DE5-8378-8C546A5B3152.png