<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>首页页面</title>
     
  </head>
  
  <body>
      <form action="ControllerServlet" method="post">
         <h3>注册</h3>
         <div id="outer">
             <div>
                 <div class="ch">用户名:</div>
                 <div class="ip">
                    <input type="text" name="username" value="${formBean.username } "/>
                    <span>${formBean.errors.username }${DBMes}</span>
                 </div>  
             </div>
             <div>
                 <div class="ch">密码:</div>
                 <div class="ip">
                    <input type="password" name="password1" />
                    <span>${formBean.errors.password1 }${DBMes}</span>
                 </div>  
             </div>
             <div>
                 <div class="ch">确认密码:</div>
                 <div class="ip">
                    <input type="password" name="password2" />
                    <span>${formBean.errors.password2 }${DBMes}</span>
                 </div>  
             </div> <br>
              <div id="bt">
                   <input type="reset" value="重置" />
                   <input type="submit" value="注册"/>
             </div>
             
         </div>
      </form>
  </body>
</html>
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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>
  <!-- 不允许用户直接来访问 -->
        <c:if test="${sessionScope.UserBean == null }">
        <jsp:forward page="/register.jsp"></jsp:forward>
        </c:if>
    
       <div id="main">
           <div id="welcom">恭喜你,登录成功</div>
           <hr>
           <div>你的信息</div>
           <div>
              <ul>
                 <li>用户名:${UserBean.username }</li>
                 <li>密码:${UserBean.password }</li>
              </ul>
           </div>
       </div>
          
  </body>
</html>
succ.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>chap12</display-name>    
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
     <description></description>
     <description>ControllerServlet</description>
     <servlet-name>ControllerServle</servlet-name>
     <servlet-class>cn.itcast.chap12.model2</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>cn.itcast.chap12.model2.web.ControllerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/ControllerServlet</url-pattern>
  </servlet-mapping>
  
  
</web-app>
web.xml
package cn.itcast.chap12.model2.domain;

public class UserBean {
       private String username;//用户名
       private String password1;//密码
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword1() {
        return password1;
    }
    public void setPassword1(String password1) {
        this.password1 = password1;
    }
       
       
}
UserBean.java
package cn.itcast.chap12.model2.domain;

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

/**
 * 用来封装用户的注册参数
 * 
 *
 */
public class RegisterFormBean {
    private String username;//用户名
    private String password1;//密码
    private String password2;//确认密码
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword1() {
        return password1;
    }
    public void setPassword1(String password1) {
        this.password1 = password1;
    }
    public String getPassword2() {
        return password2;
    }
    public void setPassword2(String password2) {
        this.password2 = password2;
    }
    
    //---------进行服务端的数据------------
    //1.定义一个变量来接收用户校验的信息,比如说用户名是否符合规范
    //第一个参数  属性名称  第二个参数:属性值是否符合规范,比如“username”,“有错误”
    private Map<String,String> errors =new HashMap<String,String>();
    public boolean validate(){
        //2.定义变量,用于判断是否满足规范,如果满足就返回true ,不满足就返回false
        boolean flag = true;
        if(username == null || username.trim().equals("")){
            errors.put("username", "请输入用户名");
            flag = false;
            return flag;
        }
        if(password1 == null || password1.trim().equals("")){
            errors.put("password1", "请输入密码");
            flag = false;
            return flag;
        }else if(password1.length()<12 || password1.length()>6){
            errors.put("password", "请输入6-12个字符");
            flag = false;
            return flag;
        }
        if(password1 != null || !password1.equals("password2")){
            errors.put("password2", "俩次输入密码不一致");
            flag = false;
            return flag;
        }
        
        return flag;
        
    }
    //获取错误信息的方法
    public Map<String, String> getErrors() {
        return errors;
    }
    //提供map集合中添加错误信息
    public void setErrorMsg(String err,String errMsg){
        if(err != null && errMsg != null){
            errors.put(err, errMsg);    
        }
    }
    
}
RegisterFormBean.java
package cn.itcast.chap12.model2.util;



import java.util.HashMap;

import cn.itcast.chap12.model2.domain.RegisterFormBean;
import cn.itcast.chap12.model2.domain.UserBean;

/**
 * 
 * 模拟数据库
 *
 */
public class DBUtil {

    private static DBUtil instance = new DBUtil();
    public static DBUtil getInstance(){
        return instance;
    }
    //定义集合,来保存用户注册的数据
    private HashMap<String,RegisterFormBean> users = new HashMap<String,RegisterFormBean>();
    //插入俩条数据
    public DBUtil(){
        RegisterFormBean user1 = new RegisterFormBean();
        user1.setUsername("litao");
        user1.setPassword1("123456");
        users.put("litao",user1 );
        RegisterFormBean user2 = new RegisterFormBean();
        user2.setUsername("rose");
        user2.setPassword1("asdfg");
        users.put("litao",user2 );    
    }
    
    //提供获取值的方法
    public RegisterFormBean getUser(String name){
        RegisterFormBean user = users.get(name);
        return user;
    }
    
    //提供插入数据库方法
    public boolean insertUser(UserBean user){
        //用户为null,不能插入
        if( user == null){
            return false;
        }
        //判断用户名是否重复
        String username = user.getUsername();//用户提交过来时注册的参数
        RegisterFormBean userDB = users.get(username);
        if(userDB != null){
            //根据注册参数查询数据有用户对象,表明用户存在
            return false;
        }
        users.put(username, userDB);
        return true;
    }
}
DBUtil.java
package cn.itcast.chap12.model2.web;

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.chap12.model2.domain.RegisterFormBean;
import cn.itcast.chap12.model2.domain.UserBean;
import cn.itcast.chap12.model2.util.DBUtil;

public class ControllerServlet extends HttpServlet {
   /**
     * 
     */
    private static final long serialVersionUID = 1L;
    

   

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.处理响应和请求的中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //2.获取用户提交的注册参数,封装到注册的JavaBean
        //BeanUtils.populate(new RrgisterFormBean(), request.getParameterMap());
        String username = request.getParameter("username");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        RegisterFormBean formBean = new RegisterFormBean();
         formBean.setUsername(username);
         formBean.setPassword1(password1);
         formBean.setPassword2(password2);
        //3.在服务器端校验数据是否规范和数据的正确性
        if(!formBean.validate()){
            //数据不规范
            request.setAttribute("formBean", formBean);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            return;
        }
        //4.把数据封装到UserBean,传递给数据库进行操作
        UserBean userBean = new UserBean();
         userBean.setUsername(username);
         userBean.setPassword1(password1);
        //5.调用DBUtil的插入方法,完成注册
        boolean b = DBUtil.getInstance().insertUser(userBean);
        //6.判断是否插入成功
        if(!b){
            //插入失败
            request.setAttribute("DBMes","用户已经存在" );
            request.setAttribute("formBean", formBean);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            return;
        }
        //7.插入成功
        response.getWriter().print("恭喜你注册成功,3秒后自动跳转");
        //为了在主页面回显用户信息,需要把用户信息存到session对象
        request.getSession().setAttribute("userBean", userBean);
        //注册成功,三秒后跳转到主页面中
        response.setHeader("refresh","3;url=scc.jsp" );
    }
        
        

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
        
       
    }

    

}
ControllerServlet.java