使用 jQuery Ajax 异步登录,并验证用户输入信息(maven) (3)

(4)UserDAO.java 文件(有更新):

package com.dao; import com.dbutil.DBUtils; import com.pojo.User; import java.util.List; public class UserDAO { public List<User> findAllUser(){ DBUtils dbUtils = new DBUtils(); return dbUtils.findAllUser(); } //返回 0 或 1 public int login(User user){ DBUtils dbUtils = new DBUtils(); //与数据库是否匹配,匹配为 1 int temp = dbUtils.login(user); return temp; } }

(5)DBUtils.java 文件:
!!! 成败在与这个文件

自行更改数据库名

自行更改表名(两个)

自行更改 root 的密码

检查服务是否启动

package com.dbutil; import com.pojo.User; import java.sql.*; import java.util.ArrayList; import java.util.List; public class DBUtils { Connection conn; //预处理 PreparedStatement pstmt; //结果集 ResultSet rs; public DBUtils(){ try{ Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","xiaopengwei"); } catch (Exception e){ e.printStackTrace(); } } public int login(User user){ try{ pstmt=conn.prepareStatement("select * from tb_user where uname = ? and upwd = md5(?)"); pstmt.setString(1, user.getUname()); pstmt.setString(2, user.getUpwd()); rs = pstmt.executeQuery(); if (rs.next()) return 1; else return 0; }catch (Exception e){ e.printStackTrace(); } return 0; } //查询数据库中 tb_user 表的所有信息 public List<User> findAllUser() { try { String sql = "select * from tb_user"; //预处理 pstmt = conn.prepareStatement(sql); //执行查询 rs = pstmt.executeQuery(); //实例化 List<User> list = new ArrayList<User>(); while (rs.next()) { User user = new User(); user.setId(rs.getInt(1)); user.setUname(rs.getString(2)); user.setUpwd(rs.getString(3)); user.setLastLoginTime(rs.getString(4)); user.setRole(rs.getString(5)); list.add(user); } return list; } catch (Exception e) { System.out.println(e.getMessage()); } return null; } }

(6)Servlet.java 文件:

package com.loginServlet; import com.pojo.User; import com.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; import java.io.PrintWriter; import java.sql.PreparedStatement; @WebServlet(name = "Servlet",urlPatterns = "/Servlet") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost"); request.setCharacterEncoding("utf-8"); //获取 post 请求中的两个参数 String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String role = request.getParameter("role"); /*硬编码, 不从数据库获取信息,直接验证 if (uname.equals("admin") && upwd.equals("123456")) { response.sendRedirect("index.jsp"); }else{ response.sendRedirect("loginServlet.html"); }*/ //从服务获取值 UserService userService = new UserService(); User user = new User(); user.setUname(uname); user.setUpwd(upwd); user.setRole(role); if (userService.login(user)) { //向服务器要数据 //登录成功跳转 index.jsp response.sendRedirect("index.jsp"); }else{ response.sendRedirect("failed.jsp"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String role = request.getParameter("role"); System.out.println("doGet"); System.out.println(uname+":"+upwd+"-"+role); UserService userService = new UserService(); User user = new User(); user.setUname(uname); user.setUpwd(upwd); user.setRole(role); boolean login = userService.login(user); PrintWriter out = response.getWriter(); if (login) out.print("1"); else out.print("0"); out.close(); } }

(7)UserServletForAjax.java 文件:

package com.dbutil; import com.pojo.User; import java.sql.*; import java.util.ArrayList; import java.util.List; public class DBUtils { Connection conn; //预处理 PreparedStatement pstmt; //结果集 ResultSet rs; public DBUtils(){ try{ Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","xiaopengwei"); } catch (Exception e){ e.printStackTrace(); } } public int login(User user){ try{ pstmt=conn.prepareStatement("select * from tb_user where uname = ? and upwd = md5(?)"); pstmt.setString(1, user.getUname()); pstmt.setString(2, user.getUpwd()); rs = pstmt.executeQuery(); if (rs.next()) return 1; else return 0; }catch (Exception e){ e.printStackTrace(); } return 0; } //查询数据库中 tb_user 表的所有信息 public List<User> findAllUser() { try { String sql = "select * from tb_user"; //预处理 pstmt = conn.prepareStatement(sql); //执行查询 rs = pstmt.executeQuery(); //实例化 List<User> list = new ArrayList<User>(); while (rs.next()) { User user = new User(); user.setId(rs.getInt(1)); user.setUname(rs.getString(2)); user.setUpwd(rs.getString(3)); user.setLastLoginTime(rs.getString(4)); user.setRole(rs.getString(5)); list.add(user); } return list; } catch (Exception e) { System.out.println(e.getMessage()); } return null; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfygg.html