login.jsp
1 <%@page contentType="text/html" pageEncoding="UTF-8" %> 2 <html> 3 <head> 4 5 6 </head> 7 8 <body> 9 <center> 10 <h1>登陆操作!</h1> 11 </center> 12 <hr> 13 <form action="login_check.jsp" method="post"> 14 <table border="1"> 15 <tr> 16 <td colspan="2">用户登陆</td> 17 18 </tr> 19 <tr> 20 <td>登陆 ID:</td> 21 <td><input type="text" name="id"> 22 </td> 23 </tr> 24 25 <tr> 26 <td>登陆密码:</td> 27 <td><input type="password" name="password"> 28 </td> 29 </tr> 30 31 </tr> 32 33 <tr> 34 <td colspan="2"><input type="submit" value = "登陆!"> 35 <td colspan="2"><input type="reset" value = "重置!"> 36 </td> 37 </tr> 38 </table> 39 40 </form> 41 </body> 42 </html>
login_check.jsp
1 <%@page contentType="text/html" pageEncoding="UTF-8" %> 2 <%@page import="java.sql.*"%> 3 4 5 <html> 6 <head> 7 </head> 8 9 <body> 10 <center> 11 <h1>登陆操作</h1> 12 </center> 13 <hr> 14 <%!//定义若干个数据库连接常量 15 public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; 16 public static final String DBURL = "jdbc:mysql://localhost:3306/zhw"; 17 public static final String DBUSER = "root"; 18 public static final String DBPASS = "root"; 19 boolean flag = false; 20 String name = null; 21 %> 22 <% 23 Connection conn = null; //数据库连接 24 PreparedStatement pstmt = null; //数据库预处理操作 25 ResultSet rs = null; //查询要处理结果集 26 %> 27 <% 28 29 try{ 30 31 %> 32 <% 33 Class.forName(DBDRIVER); 34 conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); 35 36 String sql = "select name from user where userid = ? and password = ?"; 37 pstmt = conn.prepareStatement(sql); 38 pstmt.setString(1, request.getParameter("id")); 39 40 41 pstmt.setString(2, request.getParameter("password")); 42 43 rs = pstmt.executeQuery(); 44 45 if(rs.next()){//如果有数据就执行 46 47 flag = true;//表示登陆成功 48 name = rs.getString(1); 49 50 } 51 System.out.println("如果有数据就执行22"); 52 %> 53 54 <% 55 }catch(Exception e){ 56 e.printStackTrace(); 57 }finally{ 58 try{ 59 rs.close(); 60 pstmt.close(); 61 conn.close(); 62 }catch(Exception e){ 63 64 e.printStackTrace(); 65 } 66 67 } 68 69 %> 70 71 <% 72 if(flag == true){ 73 %> 74 <jsp:forward page="login_success.jsp"> 75 <jsp:param name="uname" value="<%=name %>" /> 76 </jsp:forward> 77 78 <% 79 }else{ 80 %> 81 82 83 <jsp:forward page="login_failure.jsp"/> 84 85 86 <% 87 88 } 89 90 %> 91 92 </body> 93 </html>
login_success.jsp
1 <%@page contentType = "text/html" pageEncoding="GBK" %> 2 <html> 3 <head> 4 5 6 </head> 7 8 <body> 9 <center> 10 <h1>登陆操作</h1> 11 <h2>登陆成功</h2> 12 <h3>welcome!!!!<font color = "red" ><%= request.getParameter("uname") %></font></h3> 13 14 </center> 15 </body> 16 </html>
login_failure.jsp
1 <%@ page contentType="text/html" pageEncoding="GBK"%> 2 3 <html> 4 <head> 5 6 7 </head> 8 9 <body> 10 <center> 11 12 <h1>登陆失败!!!!!!</h1> 13 <h2><a href= "login.jsp">点击重新登陆!!!!!!!!</a></h2> 14 </center> 15 </body> 16 </html>