本次重构优化内容
1 前端页面增加JS判断
2 使用JSTL+EL替换JSP语句
3 Servlet增加用户名是否重复检查
注册页面 userReg.jsp
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户注册</title> <base href="<%=basePath%>"> <script type="text/javascript"> function surePost() { if (document.RegForm.userNum.value == "") { alert("用户名不能为空!"); return false; } if (document.RegForm.userPwd.value == "") { alert("密码不能为空!"); return false; } if (document.RegForm.userPwd.value != document.RegForm.userConfirmPwd.value) { alert("密码和确认密码输入不一致!"); return false; } return true; } </script> </head> <body> <c:choose> <c:when test="${RegResult== '1'} "> <% response.setHeader("refresh", "2;URL=login.jsp"); %> 注册成功!2秒后跳转到登录页面. </c:when> <c:otherwise> <form action="UserSvt?xtype=0" method="post" name="RegForm" onsubmit="return surePost()"> <table> <tr> <td>用 户 名:</td> <td><input type="text" name="userNum" /></td> </tr> <tr> <td>姓 名:</td> <td><input type="text" name="userName" /></td> </tr> <tr> <td>电子邮箱:</td> <td><input type="text" name="userEmail" /></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="userPwd" /></td> </tr> <tr> <td>确认密码:</td> <td><input type="password" name="userConfirmPwd" /></td> </tr> <tr> <td><input type="submit" value="注册"></td> </tr> </table> </form> <c:if test="${RegResult == '-1'}"> 注册失败。 ${RegResultInfo } </c:if> </c:otherwise> </c:choose> </body> </html>
后台Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int nXType = Integer.parseInt(request.getParameter("xtype")); if (nXType == 0) { UserItem userItem = new UserItem(); userItem.setStrUserNum(request.getParameter("userNum")); userItem.setStrUserName(TCommonTool.GetUTF8(request.getParameter("userName"))); userItem.setStrPwd(request.getParameter("userPwd")); userItem.setStrEmail(request.getParameter("userEmail")); if (!userItem.getStrPwd().equals(request.getParameter("userConfirmPwd"))) { request.setAttribute("RegResult", "-1"); request.setAttribute("RegResultInfo", "密码和确认密码输入不一致."); RequestDispatcher rd = request.getRequestDispatcher("userReg.jsp"); rd.forward(request, response); return; } DBUser dbUser = new DBUser(); dbUser.GetUser(userItem); if (userItem.getStrGUID().equals("")) { userItem.setStrGUID(java.util.UUID.randomUUID().toString()); dbUser.AddUser(userItem); request.setAttribute("RegResult", "1"); } else { request.setAttribute("RegResult", "-1"); request.setAttribute("RegResultInfo", "用户名" + userItem.getStrUserNum() + "已经存在."); } RequestDispatcher rd = request.getRequestDispatcher("userReg.jsp"); rd.forward(request, response); } }