注册页面

<%@ 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>

工具层

UserBean类

package p01.domain;

/**
 * UserBean类用于封装用户的基本参数
 * */
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 p01.domain;

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

public class RegisterFormBean {
	private String name;
	private String password ;
	private String password2;
	private String email;
	
	/*
	 * 服务端数据处理
	 * **/
	 //1,定义变量去接收用户校验信息,参数一:属性名称,参数二:属性值是否符合规范
	private Map<String ,String> errors = new HashMap<String, String>();
	public boolean validate() {
		//定义一个校验变量
		Boolean flag = true;
		//检验用户名
		if(name == null || "".equals(name.trim())) {
			errors.put("name", "请输入用户名");
			flag = false;
			return flag;
		}
		//检验密码
		if(password == null || "".equals(password.trim())) {
			errors.put("password", "请输入密码");
			flag = false;
			return flag;
		}else if(password.length()<6 || password.length()>18) {
			errors.put("password", "请输入6-18位长度的密码");
			flag = false;
			return flag;
		}else if(!password.equals(password2)) {
			errors.put("password", "与第一次输入的密码不相符");
			flag = false ;
			return flag;
		}
		//检验邮箱
		String reg = "[a-zA-Z0-9_-]+@+[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+";
		if(email == null || "".equals(email.trim())) {
			errors.put("emaill", "请输入邮箱");
			flag = false;
			return flag;
		}else if(!email.matches(reg)) {
			errors.put("email", "请输入正确的邮箱格式");
		}
		return flag ;
	}
	
	//setget方法
	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 getPassword_conform() {
		return password2;
	}
	public void setPassword_conform(String password_conform) {
		this.password2 = password_conform;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	//获取错误信息
	public Map<String, String> getErrors() {
		return errors;
	}
	public void setErrorMsg(String err,String errMsg) {
		if(err !=null && errMsg !=null) {
			errors.put(err, errMsg);
		}
	}

	//重写hashcode和equals方法
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((errors == null) ? 0 : errors.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((password2 == null) ? 0 : password2.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RegisterFormBean other = (RegisterFormBean) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (errors == null) {
			if (other.errors != null)
				return false;
		} else if (!errors.equals(other.errors))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (password2 == null) {
			if (other.password2 != null)
				return false;
		} else if (!password2.equals(other.password2))
			return false;
		return true;
	}
	
}

数据操作层

DBUtil类

package p02.util;
//用于模拟数据库

import java.util.HashMap;

import p01.domain.UserBean;

public class DBUtil {
	private static DBUtil instance= new DBUtil();
	public static DBUtil getInstace() {
		return instance;	
	}
	private HashMap<String , UserBean> users = new HashMap<String, UserBean>();
	
	//初始化
	public DBUtil() {
		UserBean u1 = new UserBean();
		UserBean u2 = new UserBean();
		u1.setName("zhangsan");
		u1.setPassword("123");
		u1.setEmail("666@666.com");
		u2.setName("lisi");
		u2.setPassword("234");
		u2.setEmail("777@777.com");
		users.put("zhangsan",u1);
		users.put("lisi",u2 );
	}
	//获取方法
	public UserBean getUser(String username) {
		UserBean user = users.get(username);
		return user; 
	}
	//插入方法
	public boolean insertUser(UserBean user) {
		if(user == null) {
			return false;
		}
		//判断数据库中是否有这个用户名
		String username = user.getName();
		UserBean userDB = users.get(username);
		//不为空就是已存在
		if(userDB != null) {
			return false;
		}
		users.put(username, user);
		return true;
		
	}
}

Web层

package p03.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 org.apache.commons.beanutils.BeanUtils;

import p01.domain.RegisterFormBean;
import p01.domain.UserBean;
import p02.util.DBUtil;


/**
 * 处理用户请求
 * */
public class ControllerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public ControllerServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//处理中文乱码
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		//获取提交的参数
		//BeanUtils.populate(bean, properties);
		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.setPassword_conform(password2);
		formBean.setEmail(email);
		//检验数据规范
		if(!formBean.validate()) {
			request.setAttribute("formBean", formBean);
			request.getRequestDispatcher("register.jsp").forward(request,response);
			return ;
		}
		//封装数据
		UserBean user_bean = new UserBean();
		user_bean.setName(name);
		user_bean.setPassword(password);
		user_bean.setEmail(email);
		//调用插入方法
		boolean b = DBUtil.getInstace().insertUser(user_bean);
		if(!b) {
			//插入失败
			request.setAttribute("DBMes","用户名已存在");
			request.setAttribute("formBean",formBean);
			request.getRequestDispatcher("register.jsp").forward(request,response);
			return ;
		}
		//插入成功
		response.getWriter().print("注册成功");
		//回显用户信息
		request.getSession().setAttribute("userBean", user_bean);
		//跳转,1秒后跳转
		response.setHeader("refresh", "1;url=loginSuccess.jsp");
				
	
	
	
	}

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

}

登陆成功回显页面

<%@ page language="java" pageEncoding="GBK" import="p01.domain.UserBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<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>
	<!-- 判断用户是否直接登陆 -->
	   <c:if test="${sessionScope.userBean == null }">
	       <jsp:forward page="register.jsp"></jsp:forward>
	   
 	   </c:if>
	    <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>
说点什么
暂时没有人评论
Loading...