博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
异步校验用户名是否存在
阅读量:7298 次
发布时间:2019-06-30

本文共 1674 字,大约阅读时间需要 5 分钟。

第一步:

  导入jquery

第二步:

  建个表单和输入框

    
输入用户名:
    

 

第三步:

  写JS代码,blur()方法

第四步:

  在web层创建Servlet(我分为web,service,dao和domain,domain负责封装,因为只有一个username参数,就没用到domain)

  以JSON格式返回isExist

public class CheckUsernameServlet extends HttpServlet {    protected void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {        //获得要校验的用户名        String username = request.getParameter("username");        //传递username到service        UserService service = new UserService();        boolean  isExist = false;        try {            isExist = service.checkUsername(username);        } catch (SQLException e) {            e.printStackTrace();        }                response.getWriter().write("{\"isExist\":"+isExist+"}");    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {        doGet(request,response);    }}

第五步:

  创建Service

public class UserService {    public boolean checkUsername(String username) throws SQLException {        UserDao dao = new UserDao();        Long isExist = dao.checkUsername(username);        return isExist>0?true:false;    }}

第六步:

  创建Dao(注:使用的是C3P0和DBUtils,所以要导入各种包和配置文件)

public class UserDao {    public Long checkUsername(String username) throws SQLException {        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());        String sql = "select count(*) from user where username = ?";        Long query = (Long) runner.query(sql, new ScalarHandler(),username);        return query;    }}

第七步,代码完成并测试

 

转载于:https://www.cnblogs.com/yg1024/p/8366495.html

你可能感兴趣的文章
远程桌面连接
查看>>
数据库三范式
查看>>
电脑是否可以有刻录机
查看>>
3 算法、控制结构
查看>>
JAVA 面试须知
查看>>
Map集合
查看>>
chsh 设置用户禁止登陆
查看>>
generatorConfig.xml
查看>>
visual studio插件开发dll类库免加全局缓存处理办法
查看>>
mysql转换表的存储引擎方法
查看>>
洛谷P1963 [NOI2009]变换序列(二分图)
查看>>
advacing lnux program --zombie process [copy]
查看>>
C# 获取windows特殊路径
查看>>
073_SFDC Limit
查看>>
009_Validation Rule 中的Vlookup使用
查看>>
jQuery 插件封装的方法
查看>>
SQLServer 添加字段分组去除重复项,用临时表
查看>>
慢速连接攻击和处理方式
查看>>
[POI2000]病毒
查看>>
item字母问题
查看>>