5.18~5.21我们的项目实现了登录注册功能
一、注册
邮箱和密码为必填信息,其他信息默认为空
<%
String uid=request.getParameter("uid");
String password=request.getParameter("password");
String confirmPassword=request.getParameter("password");
String email=request.getParameter("uid");
int gender=Integer.parseInt(request.getParameter("gender"));
String validateCode=request.getParameter("validateCode");
String realName=request.getParameter("realName");
String tel=request.getParameter("tel");
if(uid==null ||uid.trim().equals("") ||
password==null ||password.trim().equals("") ||
confirmPassword==null ||confirmPassword.trim().equals("") ||
email==null ||email.trim().equals("")){
request.setAttribute("errMsg","请将必填的数据填写完整!");
%>
<jsp:forward page="error.jsp"/>
检验两次密码填写是否一致
<%
}else if(!password.equals(confirmPassword)){
request.setAttribute("errMsg","两次密码不匹配!");
%>
<jsp:forward page="error.jsp"/>
检验用户名是否已经存在,存在则报错,不存在则创建新用户成功,将用户信息添加至用户表
<%
}
UserService service=new UserService();
boolean isExist=service.checkUid(uid);
if(isExist){
request.setAttribute("errMsg","用户名已经存在!");
%>
<jsp:forward page="error.jsp"/>
<%}
User user=new User();
user.setUid(uid);
user.setPassword(password);
user.setEmail(email);
user.setGender(gender);
user.setTel(tel);
user.setRealName(realName);
//String validateCode=Generator.getEmailCode();
user.setValidateCode(validateCode);
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//service.addUser(user,basePath);
service.addUser(user);
out.println("<script language=\"javascript\">");
out.println("alert(\"注册成功!\")");
out.println("window.location.href=\"preHome.jsp\"");
out.print("</script> ");
%>
二、登陆
检验输入用户名是否存在与库表中,并且输入的密码与用户名对应密码是否匹配,匹配则登陆成功
<%
String uid=request.getParameter("uid");
String password=request.getParameter("password");
System.out.println("==="+uid+password);
UserService userService=new UserService();
if(userService.login(uid,password)){
session.setAttribute("uid", uid);
out.println(" <script> window.close();history.go(-2); </script>");
}else{ %>
<font color="red">用户名或密码错误!</font>
<%
}%>