zoukankan      html  css  js  c++  java
  • Ajax验证用户名

    前台写法

    <script>
    $(function(){
    $("#username").blur(function(){
    //1 获得input 框用户输入的值
    var usernameInput = $(this).val();//$("#username").val();
    //alert(usernameInput);
    //2 去服务器验证用户名是否存在
    $.post(
    "${pageContext.request.contextPath}/checkUser", //1url 地址
    var usernameinfo="";
    {"username":usernameInput}, //2 传输数据
    function(data){

    var isBoolean = data.isBoolean;
    if(isBollean)}
    usernameinfo="该用户名已经存在";
    }else{
    usernameinfo="该用户可以正常使用"
    }
    $("usernameinfo").html(usernameinfo);
    }, //3 回调函数
    "json" //4 传输数据类型

    );
    });
    });
    </script>

    web层

    package cn.lijun.web;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import cn.lijun.service.UserService;

    public class CheckUserServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1 获得需要校验的用户
    String username = request.getParameter("username");
    //2 传递username到service
    UserService ser = new UserService();
    boolean isBoolean = false;
    try {
    isBoolean = service.checkUsername(username);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    //{"isBoolean":isBoolean}
    response.getWriter().write("{"isBoolean":"+isBoolean+"}");//端端 加加"++"

    }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    }
    }

    service层代码如下

    package cn.lijun.service;

    import cn.lijun.dao.UserDao;

    public boolean checkUsername(String username) throws SQLException {

    UserDao dao = new UserDao();
    Long isBoolean = dao.checkUsername(username);

    return isBoolean>0?true:false;
    }

    dao层代码如下

    package cn.lijun.dao;

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

  • 相关阅读:
    继承与 接口
    数组
    字符串加密
    类与对象
    java 方法学习
    课堂练习
    第一次课堂任务记录。整形数字相加合输出
    《大道至简》第二章 读后感
    大道至简第二章
    大道至简第一章
  • 原文地址:https://www.cnblogs.com/lijun6/p/10519883.html
Copyright © 2011-2022 走看看