2020.11.26收获
实现多用户登录
userSevlet.java
package servlet; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import bean.userbean;; public class userServlet { private Connection conn; private PreparedStatement pstmt; @SuppressWarnings("static-access") public userServlet() { conn = new util.DBUtil().getConn(); } public boolean Hefa1(userbean user) { try { pstmt = conn.prepareStatement( "select * from usertable where user= '" + user.getUser() + "' and password=? and job=?"); /* pstmt.setString(1, user.getUser()); */ System.out.println(user.getUser()); pstmt.setString(1, user.getPassword()); pstmt.setString(2, user.job1); ResultSet rs = pstmt.executeQuery(); if (rs.next()) return true; else return false; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } public boolean Hefa2(userbean user) { try { pstmt = conn.prepareStatement( "select * from usertable where user= '" + user.getUser() + "' and password=? and job=?"); /* pstmt.setString(1, user.getUser()); */ pstmt.setString(1, user.getPassword()); pstmt.setString(2, user.job2); ResultSet rs = pstmt.executeQuery(); if (rs.next()) return true; else return false; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } }
userbean.java
package bean; public class userbean { private String user; private String password; public String job1="管理员"; public String job2="读者"; public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登陆</title> <style type="text/css"> .header { height: 100px; background-color: pink; font-family:楷体; } .a { margin-left: 20px; margin-top: 80px; text-align: center; font-family:楷体; } .button { background-color: pink; border: none; height: 40px; width: 80px; font-family:楷体; } </style> </head> <body> <h1 class="header" style="text-align: center;padding-top: 40px">图书管理系统</h1> <h2 style="color: blue; text-align: center; font-family:楷体;padding-top: 40px">登陆</h2> <form action="check.jsp" method="get" onsubmit="return check()"> <table border="0" style="align: center; margin: auto;"> <div class="a"> 用户名: <input type="text" name="user" id="user"> </div> <div class="a"> 密码: <input type="password" name="password" id="password"> </div> <div class="a"> <button type="submit" class="button">登陆</button> </div> </table> </form> </body> <script type="text/javascript"> function check() { var user=document.getElementById("user"); var password=document.getElementById("password"); if(user.value==""||mima.value=="") { alert("请把信息填写完整!"); return false; } } </script> </html>
check.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <jsp:useBean id="a" class="bean.userbean"></jsp:useBean> <jsp:useBean id="b" class="servlet.userServlet"></jsp:useBean> <jsp:setProperty property="*" name="a" /> <body> <% request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); System.out.println(request.getParameter("user")); if (b.Hefa1(a)) { %> <jsp:forward page="guanliyuan.jsp"></jsp:forward> <% } else if (b.Hefa2(a)) { session.setAttribute("user", a); System.out.println(a.getUser()); %> <jsp:forward page="reader.jsp"></jsp:forward> <% } else { %> <jsp:forward page="login.jsp"></jsp:forward> <% } %> </body> </html>