JavaWeb网上图书商城完整项目--day02-5.ajax校验功能之服务器端三层实现
regist.jsp页面中有异步请求服务器来对表单进行校验:
l 校验登录名是否已注册过;
l 校验Email是否已注册过;
l 校验验证码是否正确。
这说明在UserServlet中需要提供相应的方法来支持前端的请求。
我们需要到数据库查询用户名、邮箱是否注册,到session中检查验证码是否正确。
在进行数据库操作之前,还需要对user表中的字段进行添加处理
因为其他页面中对用户的操作还设计到修改新的密码、确认密码、验证码等几个字段,我们需要在user表中添加下面的几个字段
package com.weiyuan.goods.user.domian; public class User { private String uid; //主键 private String loginname;// 登陆名称 private String loginpass;// 登陆密码 private String email;//注册的邮箱 private String verifyCode; //验证码 private boolean status;//是否激活 private String activationCode;//激活码 //增加下面的几个字段 private String reloginpass; //确认密码 private String newloginpass;//修改密码对应的新密码 public String getUid() { return uid; } public String getReloginpass() { return reloginpass; } public void setReloginpass(String reloginpass) { this.reloginpass = reloginpass; } public String getNewloginpass() { return newloginpass; } public void setNewloginpass(String newloginpass) { this.newloginpass = newloginpass; } public void setUid(String uid) { this.uid = uid; } public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public String getLoginpass() { return loginpass; } public void setLoginpass(String loginpass) { this.loginpass = loginpass; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getVerifyCode() { return verifyCode; } public void setVerifyCode(String verifyCode) { this.verifyCode = verifyCode; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public String getActivationCode() { return activationCode; } public void setActivationCode(String activationCode) { this.activationCode = activationCode; } @Override public String toString() { return "User [uid=" + uid + ", loginname=" + loginname + ", loginpass=" + loginpass + ", email=" + email + ", verifyCode=" + verifyCode + ", status=" + status + ", activationCode=" + activationCode + "]"; } }
我们来看dao层的代码:
package com.weiyuan.goods.user.dao; import java.sql.SQLException; import org.apache.commons.dbutils.handlers.ScalarHandler; import cn.itcast.jdbc.TxQueryRunner; public class UserDao { //操作数据库 private TxQueryRunner qr = new TxQueryRunner(); /*** * 查询用户名是否存在 * @throws SQLException */ public boolean ajaxValidateLoginName(String loginName) throws SQLException{ //获得满足记录的数目是对象,返回一个整数,整数是单行单列使用ScalarHandler String sql ="select count(*) from t_user where loginname=?"; Number num = (Number) qr.query(sql, new ScalarHandler(),loginName); int count = num.intValue(); if(count>0){ return true; } return false; } /*** * 查询邮箱是否存在 * @throws SQLException */ public boolean ajaxValidateEmain(String email) throws SQLException{ //获得满足记录的数目是对象,返回一个整数,整数是单行单列使用ScalarHandler String sql ="select count(*) from t_user where email=?"; Number num = (Number) qr.query(sql, new ScalarHandler(),email); int count = num.intValue(); if(count>0){ return true; } return false; } }
我们来看业务层的代码:
package com.weiyuan.goods.user.service; import java.sql.SQLException; import javax.management.RuntimeErrorException; import com.weiyuan.goods.user.dao.UserDao; public class UserService { private UserDao dao = new UserDao(); public boolean ajaxValidateLoginName(String loginName) { try { return dao.ajaxValidateLoginName(loginName); } catch (SQLException e) { // TODO Auto-generated catch block throw new RuntimeException(e.getMessage()); } } public boolean ajaxValidateEmail(String email) { try { return dao.ajaxValidateLoginName(email); } catch (SQLException e) { // TODO Auto-generated catch block throw new RuntimeException(e.getMessage()); } } }
我们来看servlet的代码:
package com.weiyuan.goods.user.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.weiyuan.goods.user.service.UserService; import cn.itcast.servlet.BaseServlet; /** * Servlet implementation class UserServlet */ @WebServlet("/UserServlet") public class UserServlet extends BaseServlet{ private static final long serialVersionUID = 1L; private UserService service = new UserService(); public String validateLoginname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //首先获得用户上传的用户名 String loginName = request.getParameter("loginname"); boolean flag = service.ajaxValidateLoginName(loginName); response.getWriter().print(flag); return null; } public String validateEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获得用户上传的emai String email = request.getParameter("email"); boolean flag = service.ajaxValidateEmail(email); response.getWriter().print(flag); return null; } public String validateVerifyCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获得用户上传的verfycode String verifyCode = request.getParameter("verifyCode"); //获得session中保存的验证码 String sessionCode = (String) request.getSession().getAttribute("vCode"); //二者进行比较看是否相等 boolean flag = sessionCode.equalsIgnoreCase(verifyCode); response.getWriter().print(flag); return null; } public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("regist is called"); return null; } }
posted on 2017-05-05 20:48 luzhouxiaoshuai 阅读(689) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!