1.創建index.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.*"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type = "text/css"> a:link,a:visited{ color:#0044DD; text:-decoration:none;} table th{ color:#2467fa; background:#dadada; font-size:16px; border:1px solid #fffff; } table td{ background:#dadada; text-align:center; border-align:1px solid #ffffff; } </style> <title>主界面</title> </head> <body> <div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa"> <h1 style="font-family:仿宋">賬號信息管理系統</h1> <table align="center" width="400px" border="1"> <tr><th>用戶名</th><th>密碼</th><th>年齡</th><th>操作</th></tr> <% Class.forName("com.mysql.cj.jdbc.Driver"); //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123"); String sql = "select * from userinfo"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { %> <tr><td><%=rs.getString("name") %></td> <td><%=rs.getString("password") %></td> <td><%= rs.getInt("age") %></td> <td><button type = "button" onclick="alert('請先登陸!')">修改</button> <button type = "button" onclick="alert('請先登陸!')">刪除</button> </td> </tr> <% } rs.close(); stmt.close(); con.close(); %> </table> <br> <button type = "button" align = "center" onclick="window.location.href='login.jsp'" ><h4>登陸</button> <br><br> </div> </body> </html> 2.創建login.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type = "text/css"> a:link,a:visited{ color:#0044DD; text:-decoration:none;} table th{ color:#2467fa; background:#dadada; font-size:16px; border:1px solid #fffff; } table td{ background:#dadada; text-align:center; border-align:1px solid #ffffff; } </style> <title>登陸界面</title> </head> <body> <div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa"> <h1 style="font-family:仿宋">賬號信息管理系統</h1> <form name = "form1" action = "login_do.jsp" method="POST"> <table align="center" width="300px" border="1"> <tr><th>用戶名</th> <td><input type = "text" name = "username"></td> </tr> <tr><th>密碼</th> <td><input type = "password" name = "password"></td> </tr> <tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" > <input type = "submit" value = "登陸"></th> </tr> </table> </form> <br> </div> </body> </html> 3.創建login_success.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.*" %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type = "text/css"> a:link,a:visited{ color:#0044DD; text:-decoration:none;} table th{ color:#2467fa; background:#dadada; font-size:16px; border:1px solid #fffff; } table td{ background:#dadada; text-align:center; border-align:1px solid #ffffff; } </style> <title>登陸成功</title> </head> <body> <%String name1 = (String)session.getAttribute("username"); if(name1 == null) { String mgs = "您尚未登陸,請先登陸,3秒後跳轉登陸界面!";%> <h1 align="center" /> <%=mgs %> <% response.setHeader("Refresh", "3;login.jsp"); } else{ %> <div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa"> <h1 style="font-family:仿宋">賬號信息管理系統</h1> <h2 style="font-family:仿宋"><%=name1 %>,歡迎您!</h2> <table align="center" width="400px" border="1"> <tr><th>用戶名</th><th>密碼</th><th>年齡</th><th>操作</th></tr> <% Class.forName("com.mysql.cj.jdbc.Driver"); //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123"); String sql = "select * from userinfo"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { %> <tr><td><%=rs.getString("name") %></td> <td><%=rs.getString("password") %></td> <td><%= rs.getInt("age") %></td> <td><input type="button" onclick="window.location.href='edit.jsp?name=<%=rs.getString("name") %>'" value="修改"> <a href="del.jsp?name=<%=rs.getString("name") %>" onclick="return confirm('確定刪除嗎?')"><input type = "button" value="刪除"></a> </td> </tr> <% } rs.close(); stmt.close(); con.close(); %> </table> <br> <table align="center" > <tr> <td><input type="button" onclick="window.location.href='query_paper.jsp'" value="查詢論文"></td> <td> </td> <td><input type="button" onclick="window.location.href='add.jsp'" value="增加用戶"></td> <td> </td> <td><input type="button" onclick="window.location.href='query.jsp'" value="查詢用戶"></td> <td> </td> <td><a href="login_out.jsp" onclick="return confirm('確定退出嗎?')"><input type="button" value="退出登陸"></a></td> </tr> </table> <br> </div> <%} %> </body> </html>
- 創建login_out.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>退出登陸處理界面</title> </head> <body> <%session.setAttribute("username",null); String mgs = "退出成功,3秒後跳轉到主界面!";%> <h1 align="center" /><%=mgs %> <% response.setHeader("Refresh", "3;index.jsp");%> </body> </html>
- 創建login_do.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.*"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登陸驗證頁面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("username"); String password = request.getParameter("password"); String sql = "select count(*) from userinfo where name = ? and password = ?"; Class.forName("com.mysql.cj.jdbc.Driver"); //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123"); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2,password); ResultSet rs = pstmt.executeQuery(); String a = "1"; while(rs.next()) { color:#3f7f7f;">body> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("username"); String password = request.getParameter("password"); String sql = "select count(*) from userinfo where name = ? and password = ?"; Class.forName("com.mysql.cj.jdbc.Driver"); //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123"); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2,password); ResultSet rs = pstmt.executeQuery(); String a = "1"; while(rs.next()) { if(a.equals(rs.getString("count(*)"))) { session.setAttribute("username", name); %> <jsp:forward page="login_success.jsp"></jsp:forward> <% } else { String mgs = "登錄失敗,3秒後跳轉到主界面!";%> <h1 align="center" /> <%=mgs %> <% response.setHeader("Refresh", "3;index.jsp"); } } rs.close(); pstmt.close(); con.close(); %> </body> </html>
- 創建query.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type = "text/css"> Class.forName("com.mysql.cj.jdbc.Driver"); //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","laotu123"); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2,password); ResultSet rs = pstmt.executeQuery(); String a = "1"; while(rs.next()) { if(a.equals(rs.getString("count(*)"))) { session.setAttribute("username", name); %> <jsp:forward page="login_success.jsp"></jsp:forward> <% } else { String mgs = "登錄失敗,3秒後跳轉到主界面!";%> <h1 align="center" /> <%=mgs %> <% response.setHeader("Refresh", "3;index.jsp"); } } rs.close(); pstmt.close(); con.close(); %> </body> </html>
- 創建query.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type = "text/css"> a:link,a:visited{ color"laotu123"); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2,password); ResultSet rs = pstmt.executeQuery(); String a = "1"; |