1.1首页index.jsp
代码如下(示例):
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="login.jsp">登录界面</a>
<a href="login2.jsp">登录界面2</a>
<a href="ajax2_1.html">登录界面2测试</a>
<a href="ajax2_2.html">登录界面测试***</a>
</body>
</html>
2.静态界面ajax2_1.html
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
function ajaxaaa(){
var acc = document.getElementById("acc").value;
//根据不同的浏览器,实例化相应的异步对象
var xmlhttp; //它是ajax的核心对象
if (window.XMLHttpRequest){
//新的客户端环境
xmlhttp = new XMLHttpRequest();
}
else{
//旧的客户端环境
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//建立和服务器的连接
/*第1个参数:提交方式(get、post)
第2个参数:请求的servlet
第3个参数:同步or异步(true:异步 false:同步)
*/
xmlhttp.open("get", "ajax2_1?acc="+acc,true);
xmlhttp.send(); //发送请求
//等待服务器响应完请求
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
var msg = xmlhttp.responseText;
document.getElementById("sp").innerHTML = msg;
}
}
}
</script>
</head>
<body>
<div>用户名:<input id="acc" type="text" onblur="ajaxaaa()"></div>
<div><span id="sp"></span></div>
<div>密码:<input id="pwd" type="text"></div>
</body>
</html>
3.响应servlet
package web;
import com.yl.pojo.User;
import com.yl.service.UserService;
import com.yl.service.impl.UserServiceimpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
/**
* 杨露
**/
@WebServlet(name = "ajax",urlPatterns = "/ajax2_1")
public class ajax2_1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request,response );
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取用户填写的账号,然后去匹配数据库,此处省略....
String acc = request.getParameter("acc");
// UserService service = new UserServiceimpl();
// boolean ok = service.exists(acc);
// UserService userService = new UserServiceimpl();
// boolean ok = userService.findUserByName(acc);
// response.getWriter().print(ok);
//假设admin已经被注册
if ("root".equals(acc)){
response.getWriter().print("<font style='color:red;font-weight:900'>很遗憾,该账号已经被注册</font>");
// response.getWriter().print("<script> alert('很遗憾,该账号已经被注册');</script>");
}
else{
response.getWriter().print("<font style='color:green;font-weight:900'>恭喜,该账号可以被注册</font>");
}
}
catch (Exception e){
}
}
}
3.从数据库中查找并验证
德鲁伊数据库
1.导入jar包,并将其添加为lib库
2.jdbc.properties
driverClassName=com.mysql.jdbc.Driver
#url=jdbc:mysql://localhost:3306/test
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=root
#初始化连接的数量:不宜超过8
initialSize=5
#设置最大等待时间
maxWait=2000
3.JdbcUtils连接数据库
public class JdbcUtils {
private static DataSource ds;
static{
try {
Properties properties = new Properties();
properties.load(JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
ds = DruidDataSourceFactory.createDataSource(properties);
}
catch (Exception e){
}
}
public static Connection getConnection() throws Exception{
return ds.getConnection();
}
}
4.测试package com.yl.utils;
import com.yl.service.UserService;
import com.yl.service.impl.UserServiceimpl;
import org.junit.Test;
import java.sql.Connection;
public class testOne {
Connection con = null;
@Test
public void testone(){
try {
con = JdbcUtils.getConnection();
System.out.println(con);
UserService userService = new UserServiceimpl();
System.out.println( userService.findUserByName("root"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
pojo实体类User
package com.yl.pojo;
public class User {
private String username ;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(){
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
4.查询功能的具体实现
1.dao层
1.1接口UserDao
package com.yl.dao;
import com.yl.pojo.User;
public interface UserDao {
public User selectAll() throws Exception;
public boolean findUserByName(String name) throws Exception;
}
2.实现类
UserDaoimpl`
package com.yl.dao.impl;
import com.yl.dao.UserDao;
import com.yl.pojo.User;
import com.yl.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.util.List;
public class UserDaoimpl implements UserDao {
private Connection con = null;
// private QueryRunner queryRunner;
private QueryRunner queryRunner = null;
public UserDaoimpl(){
try {
con = JdbcUtils.getConnection();
queryRunner = new QueryRunner();
}catch (Exception e){
}
}
@Override
public User selectAll() throws Exception{
String sql = "select * from admin";
BeanListHandler<User> handler = new BeanListHandler<User>(User.class);
User user = (User)queryRunner.query(con,sql,handler);
return user;
}
@Override
public boolean findUserByName(String name) throws Exception {
String sql = "select * from admin where username=?";
Object[] arr={name};
BeanListHandler<User> handler = new BeanListHandler<User>(User.class);
List<User> users = queryRunner.query(con,sql,handler,arr);
Boolean ok = (users.size()>0)?true:false;
return ok;
}
}
服务层;
UserService接口
package com.yl.service;
import com.yl.pojo.User;
public interface UserService {
public User seleteAll() throws Exception;
public boolean findUserByName(String name) throws Exception;
}
实现类UserServiceimpl
package com.yl.service.impl;
import com.yl.dao.UserDao;
import com.yl.dao.impl.UserDaoimpl;
import com.yl.pojo.User;
import com.yl.service.UserService;
/**
* 杨露
**/
public class UserServiceimpl implements UserService {
@Override
public User seleteAll() throws Exception{
UserDao userDao = new UserDaoimpl() ;
User user = (User) userDao.selectAll();
return user;
}
@Override
public boolean findUserByName(String name) throws Exception {
UserDao userDao = new UserDaoimpl();
return userDao.findUserByName(name);
}
}