ajax登录校验
Posted 池鱼i_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax登录校验相关的知识,希望对你有一定的参考价值。
分为 dao层 service层 servlet 层 连接数据库 jar包 连接数据库的 js
UserDao代码
package cn.hp.dao;
import cn.hp.util.JDBCUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public int selectByAccountAndPassword(String account, String password) {
String sql = "select count(*) from users where account=? and password=?";
Object[] objects = {account, password};
ResultSet resultSet = JDBCUtils.DQL(sql,objects);
int select=0;
try {
while (resultSet.next()){
select= resultSet.getInt(1);
}
}catch (SQLException e){
e.printStackTrace();
}
return select;
}
public int selectByAccount(String account) {
String sql = "select count(*) from users where account=? ";
Object[] objects = {account};
ResultSet resultSet = JDBCUtils.DQL(sql,objects);
int select=0;
try {
while (resultSet.next()){
select= resultSet.getInt(1);
}
}catch (SQLException e){
e.printStackTrace();
}
return select;
}
}
service层代码
package cn.hp.service;
import cn.hp.dao.UserDao;
public class UserService {
UserDao userDao= new UserDao();
public boolean login(String account, String password) {
int select= userDao.selectByAccountAndPassword(account,password);
if (select>0){
return true;
}else {
return false;
}
}
public boolean check(String account) {
int select= userDao.selectByAccount(account);
if (select>0){
return true;
}else {
return false;
}
}
}
servlet层代码
package cn.hp.servlet;
import cn.hp.service.UserService;
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 java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//获取用户输入的内容
String account= req.getParameter("account");
String password= req.getParameter("password");
//根据service来判断当前登录的账号密码是否正确
UserService userService =new UserService();
boolean flag= userService.login(account,password);
if (flag){
req.getRequestDispatcher("success.jsp").forward(req,resp);
}else{
req.setAttribute("msg","密码错误请重新登录");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
}
package cn.hp.servlet;
import cn.hp.service.UserService;
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 java.io.IOException;
@WebServlet("/check")
public class CheckServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//获取用户输入的内容
String account= req.getParameter("account");
//根据service来判断当前登录的账号密码是否正确
UserService userService =new UserService();
boolean flag= userService.check(account);
resp.getWriter().println(flag);
System.out.println(flag);
}
}
uitl代码块
package cn.hp.util;
import java.sql.*;
public class JDBCUtils {
static Connection connection = null ;
static Statement statement = null ;
static PreparedStatement preparedStatement = null ;
static ResultSet resultSet = null ;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?characterEncoding=utf8&serverTimezone=UTC", "root", "123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// 新增和修改 DML 操作 String sql , sql中的参数 preparedStatement Object[] arr 用来补全sql中的?
public static int DML(String sql , Object[] arr ){ // 可变参数 Object ... arr 0~n个参数
int update = 0 ;
try{
preparedStatement = connection.prepareStatement(sql);
// 循环完毕,会将sql中全部? 补充齐
for (int i = 0 ; i < arr.length ; i++){
preparedStatement.setObject(i+1 , arr[i] );
}
update = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return update ;
}
// 带参查询 DQL 操作
public static ResultSet DQL(String sql , Object[] arr ){
try{
preparedStatement = connection.prepareStatement(sql);
// 循环完毕,会将sql中全部? 补充齐
for (int i = 0 ; i < arr.length ; i++){
preparedStatement.setObject(i+1 , arr[i] );
}
resultSet = preparedStatement.executeQuery();
}catch (SQLException e){
e.printStackTrace();
}
return resultSet ;
}
// 单独提供关闭资源方法
public static void close(){
try {
if (statement!=null){
statement.close();
}
if (connection!= null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Show.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆页面</title>
</head>
<script src="js/jquery-3.6.0.js"></script>
<body>
<form action="login">
账号:<input id="input1" type="text" name="account"> <span id="span1"></span> <br/>
密码:<input type="password" name="password"><span style="color: red">${msg}</span><br/>
<input type="submit" value="登录">
</form>
</body>
<script>
$("#input1").blur(function () {
let account = $("#input1").val();
$.ajax({
url:"check",
type:"get",
data:"account="+account,
success:function (data) {
let data1 = JSON.parse(data);
if (data1){
$("#span1").html("用户名存在");
$("#span1").attr("style","color:green");
}else {
$("#span1").html("用户名不存在");
$("#span1").attr("style","color:red");
}
}
})
})
</script>
</html>
以上是关于ajax登录校验的主要内容,如果未能解决你的问题,请参考以下文章
用户管理的设计--4.jquery的ajax实现登录名的校验
第65篇 AJAX初识 校验用户名 登录示例 文件上传 csrftoken