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   luzhouxiaoshuai  阅读(689)  评论(0编辑  收藏  举报

编辑推荐:
· 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代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示