1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>歡迎頁面</title> 8 </head> 9 <body> 10 <% 11 session.invalidate();//銷毀session 12 13 %> 14 15 <a href="denglu.jsp" >親,請登錄</a> 16 <a href="zhuce.jsp">注冊</a> 17 </body> 18 </html>
歡迎界面:
尚未注冊,直接登錄的時候:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>登錄</title> 8 </head> 9 <body> 10 <form action="test_denglu.jsp" method="post"> 11 <table width="300" height="300" border="0" align="center"> 12 <tr height="80" align="center"> 13 <td colspan="2"><font size="24">OO登錄</font></td></tr> 14 <tr height="30"> 15 <td width="80" align="right">用戶名:</td> 16 <td><input type="text" name="username"></td></tr> 17 <tr height="30"> 18 <td width="80" align="right"> 密 碼:</td> 19 <td> <input type="password" name="password"></td></tr> 20 <tr><td height="40" align="center" colspan="2"> 21 <input type="submit" value="登錄"> 22 </td></tr> 23 </table> 24 </form> 25 </body> 26 </html>
進入注冊界面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>注冊</title> 8 </head> 9 <body> 10 <form action="test_zhuce.jsp" method="post"> 11 <table width="300" height="300" border="0" align="center"> 12 <tr height="80" align="center"> 13 <td colspan="2"><font size="24">OO注冊</font></td></tr> 14 <tr height="30"> 15 <td width="80" align="right">用戶名:</td> 16 <td><input type="text" name="username"></td></tr> 17 <tr height="30"> 18 <td width="80" align="right"> 密 碼:</td> 19 <td> <input type="password" name="password"></td></tr> 20 <tr><td height="40" align="center" colspan="2"> 21 <input type="submit" value="提交"> 22 </td></tr> 23 </table> 24 </form> 25 </body> 26 </html>
注冊成功,跳轉登錄界面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" import="java.sql.*"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>檢測跳轉中</title> 8 </head> 9 <body> 10 <% 11 String name = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"); 12 String pw = request.getParameter("password"); 13 14 String str = "select * from USERS WHERE username = '" + name + "'"; 15 String str1 = "insert into users values(xuhao.nextval,'" + name + "','" + pw +"')"; 16 17 try{ 18 19 //連接數據庫 20 Connection conn = null; 21 Class.forName("oracle.jdbc.driver.OracleDriver"); 22 String strURL = "jdbc:oracle:thin:@localhost:1521:SP"; 23 conn = DriverManager.getConnection(strURL, "test", "123"); 24 System.out.println("數據庫連接成功"); 25 Statement st = conn.createStatement(); 26 ResultSet rs = st.executeQuery(str); 27 if(rs.next()){ 28 if(name.equals(rs.getString(2))){ 29 out.println("對不起,用戶名已使用。"); 30 out.println("<br>建議使用用戶名:" + name +"99"); 31 response.setHeader("refresh", "5;URL=zhuce.jsp"); 32 } 33 }else{ 34 int a = st.executeUpdate(str1); 35 if(a == 1){ 36 out.println("恭喜你,注冊成功"); 37 session.setAttribute("username", name); 38 response.setHeader("refresh", "3;URL=denglu.jsp"); 39 } 40 } 41 42 rs.close(); 43 st.close(); 44 conn.close(); 45 }catch(Exception e){ 46 e.printStackTrace(); 47 } 48 %> 49 <br><br> 50 52 </body> 53 </html>
登錄時,密碼出錯:5秒後重新登錄
賬號密碼輸入正確,進入主頁面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>主頁面</title> 8 </head> 9 <body> 10 主頁面 11 <% 12 Object obj = session.getAttribute("username"); 13 if(obj != null){ 14 String str = obj.toString(); 15 out.println(str + "登陸成功"); 16 }else{ 17 out.println("登錄超時,請重新登錄"); 18 response.setHeader("refresh", "5;URL=denglu.jsp"); 19 } 20 %> 21 <br> 22 23 <a href="yemian.jsp">退出賬號</a> 24 25 </body> 26 </html>