• 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;
        }
    
    
    }
  • 相关阅读:
    Java多线程、并发
    Java I/O系统
    Java注解
    Arthas(Java诊断工具)
    Java泛型
    Java内部类
    libpcap使用
    python文件服务器
    设计模式
    protobuf
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/6814873.html
Copyright © 2020-2023  润新知