• javaweb注册页面学习之路(二)


    本次重构优化内容

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

      

  • 相关阅读:
    课时8:环绕通知
    课时7:后置通知、异常通知
    课时6::AOP、execution表达式、前置通知
    课时:5 使用注解实现声明式事务
    课时22::PageHelper分页插件
    课时21 :使用MyBatis实现批量操作
    课时4:特殊值的注入问题和各种类型的自动装配
    课时3:三种方式的依赖注入、给各种集合类型的属性注入
    课时2:解耦合发展史、控制反转、依赖注入
    课时1:Spring环境搭建、STS工具、第一个Spring程序
  • 原文地址:https://www.cnblogs.com/tsolarboy/p/7356755.html
Copyright © 2020-2023  润新知