本次重構優化內容 1 前端頁面增加JS判斷 2 使用JSTL+EL替換JSP語句 3 Servlet增加用戶名是否重覆檢查 註冊頁面 userReg.jsp 後臺Servlet ...
本次重構優化內容
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); } }