Ajax login authentication implementation code
- 2021-10-25 06:54:24
- OfStack
Directory
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_system?characterEncoding=utf8&serverTimezone=UTC", "root", "root");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// Addition and Modification DML Operation String sql , sql Parameters in preparedStatement Object[] arr Used to complete sql In ?
public static int DML(String sql , Object[] arr ){ // Variable parameter Object ... arr 0~n Parameters
int update = 0 ;
try{
preparedStatement = connection.prepareStatement(sql);
// When the loop is completed, the sql All in ? Complement
for (int i = 0 ; i < arr.length ; i++){
preparedStatement.setObject(i+1 , arr[i] );
}
update = preparedStatement.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return update ;
}
// Query with parameters DQL Operation
public static ResultSet DQL(String sql , Object[] arr ){
try{
preparedStatement = connection.prepareStatement(sql);
// When the loop is completed, the sql All in ? Complement
for (int i = 0 ; i < arr.length ; i++){
preparedStatement.setObject(i+1 , arr[i] );
}
resultSet = preparedStatement.executeQuery();
}catch (SQLException e){
e.printStackTrace();
}
return resultSet ;
}
// Provide a separate method to close resources
public static void close(){
try {
if (statement!=null){
statement.close();
}
if (connection!= null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package cn.hp.service;
import cn.hp.dao.UserDao;
public class UserService {
public boolean login(String account, String password) {
//service Layer, depending on the dao Layer Make a 1 Article sql Execute
UserDao userDao = new UserDao();
int select = userDao.selectByAccountAndPassword(account ,password);
if (select>0){
return true;
}else {
return false;
}
}
public boolean check(String account) {
//service Layer, depending on the dao Layer Make a 1 Article sql Execute
UserDao userDao = new UserDao();
int select = userDao.selectByAccount(account);
if (select>0){
return true;
}else {
return false;
}
}
}
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 {
//1. Set the encoding format of user input, because it may be Chinese
req.setCharacterEncoding("utf-8");
//2. Get the content of the number of users input
String account = req.getParameter("account");
String password = req.getParameter("password");
//3. Basis service To judge whether the password of the current login account is correct
//User Table Definition 1 A UserService Used to deal with All and user Related business
UserService userService = new UserService();
//login Method to pass in the account password Returns whether the login was successful
boolean flag = userService.login(account,password);
//4. Jump the control page according to whether the login is successful or not
if (flag){
req.getRequestDispatcher("success.jsp").forward(req,resp);
}else {
req.setAttribute("msg"," The password is wrong, please log in again! ");
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 {
//1. Set the encoding format of user input, because it may be Chinese
req.setCharacterEncoding("utf-8");
//2. Get the content of the number of users input
String account = req.getParameter("account");
//3. Basis service To judge whether the password of the current login account is correct
//User Table Definition 1 A UserService Used to deal with All and user Related business
UserService userService = new UserService();
// Use check Method Check out whether there is a user true User exists false Nonexistent
boolean flag = userService.check(account);
resp.getWriter().println(flag);
}
}
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) {
// Execute select Query sql Statement
String sql = "select count(*) from user 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) {
// Execute select Query sql Statement
String sql = "select count(*) from user 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;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> Login page </title>
<script src="js/jquery-3.6.0.js"></script>
</head>
<body>
<form action="login">
Account number: <input id="input1" type="text" name="account" /> <span id="span1"></span> <br/>
Password: <input type="password" name="password" /> <span style="color:red"> ${msg } </span><br/>
<input type="submit" value=" Login "/>
</form>
</body>
<script>
// Use jQuery Finish ajax Calibration Does the current account exist
$("#input1").blur(function () {
// Send ajax Request, get whether the currently entered account exists, and write it to span1 In the label
let account = $("#input1").val();
$.ajax({
url:"check",
// data:{"account":$("#input1").val() },
data:"account="+account ,
type:"get",
success:function (data) {
let data1 = JSON.parse(data);
if (data1){
// Indicates that the current account number exists
// You can log in
$("#span1").html(" You can log in ");
$("#span1").attr("style","color:green");
}else {
$("#span1").html(" Not registered ");
$("#span1").attr("style","color:red");
}
}
})
})
</script>
</html>