ajax - 完成异步用户名校验和异步自动填充.

01_异步用户名校验.

效果如下: 不刷新页面 , 输完用户名之后 , 发送一次请求 , 进行判断 .
ajax - 完成异步用户名校验和异步自动填充._第1张图片

准备工作:

1.首先要创建数据库 , 并初始化表中信息 .
2.导入工具类 和 配置文件.
3.创建好三层架构 , 就ok了.

html页面: aaa.html




    
    Title
    
    
    


    
用户名:
密码:

web层: CheckServlet.java

@WebServlet("/check")
public class CheckServlet extends HttpServlet {
    private CheckService cs = BeanFactory.newInstance(CheckService.class);

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        // 获取用户输入的用户名 .
        String username = request.getParameter("username");
        // 调用方法.
        int count = cs.check(username);
        // 看是否查到数据. 查到 - 1 , 没有 0 .
        if (count==1){
            response.getWriter().print(1);
        }else {
            response.getWriter().print(0);
        }
    }
}

dao层: UserDaoImpl.java

public class CheckDaoImpl implements CheckDao {
    @Override
    public int selectName(String username) {
        // 创建qr对象.
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        // sql语句.
        String sql = "select count(*) from user where name=? ";
        // 执行sql语句.
        try {
            // 因为jdbc底层默认是long类型 , 所以要转换为int类型.
            return ((Long)qr.query(sql,new ScalarHandler(),username)).intValue();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

domain层: user.java

public class User implements Serializable{
    private int id;
    private String name;
	// 编写geter/setter方法 .
}

02_异步自动填充:

效果如下: 自动填充数据库中存的数据 , 前提是不刷新页面.
ajax - 完成异步用户名校验和异步自动填充._第2张图片

jsp页面: bbb.html






	
	
Insert title here


	

搜索

servlet层: Search.java

@WebServlet("/search")
public class KwServlet extends HttpServlet {
    private KwService ks = BeanFactory.newInstance(KwService.class);

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        // 获取参数.
        String content = request.getParameter("content");

        // 使用KwService中的方法.
        List list = ks.findByContent(content);

        // 将查询到的数据 , 转换为json格式.
        String s = JSONArray.fromObject(list).toString();
        response.getWriter().print(s);
    }
}

dao层: search.java

public class KwDaoImpl implements KwDao {
    @Override
    public List selectByContent(String content) {
        // 创建QueryRunner对象.
        QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
        // 创建sql语句.
        String sql = "SELECT * FROM kw WHERE NAME LIKE ? ";
        try {
            return qr.query(sql, new BeanListHandler<>(KW.class),"%"+content+"%");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

domain层

public class KW {
    private int id;
    private String name;
    // getter / setter方法. 
}

你可能感兴趣的:(ajax - 完成异步用户名校验和异步自动填充.)