使用JSP實現用戶登錄


本文講述使用JSP實現用戶登錄,包括用戶登錄、注冊和退出功能等。

1.系統用例圖

2.頁面流程圖

3.數據庫設計

本例使用oracle數據庫

創建用戶表

包括id,username,password和email,共4個字段

 

[sql]  view plain  copy
 
  1. -- Create table  
  2. create table P_USER  
  3. (  
  4.   id       VARCHAR2(50) not null,  
  5.   username VARCHAR2(20),  
  6.   password VARCHAR2(20),  
  7.   email    VARCHAR2(50)  
  8. )  
  9. tablespace USERS  
  10.   pctfree 10  
  11.   initrans 1  
  12.   maxtrans 255  
  13.   storage  
  14.   (  
  15.     initial 64  
  16.     minextents 1  
  17.     maxextents unlimited  
  18.   );  
  19. -- Add comments to the table   
  20. comment on table P_USER  
  21.   is '用戶表';  
  22. -- Add comments to the columns   
  23. comment on column P_USER.id  
  24.   is 'id';  
  25. comment on column P_USER.username  
  26.   is '用戶名';  
  27. comment on column P_USER.password  
  28.   is '密碼';  
  29. comment on column P_USER.email  
  30.   is 'email';  

 

4.頁面設計

 

4.1登錄頁面

login.jsp

 

[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>登錄頁面</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     <form action="login_action.jsp" method="post">  
  26.         <table>  
  27.             <tr>  
  28.                 <td colspan="2">登錄窗口</td>  
  29.             </tr>  
  30.             <tr>  
  31.                 <td>用戶名:</td>  
  32.                 <td><input type="text" name="username" />  
  33.                 </td>  
  34.             </tr>  
  35.             <tr>  
  36.                 <td>密碼:</td>  
  37.                 <td><input type="password" name="password" />  
  38.                 </td>  
  39.             </tr>  
  40.             <tr>  
  41.                 <td colspan="2"><input type="submit" value="登錄" /> <href="register.jsp">注冊</a>  
  42.                 </td>  
  43.             </tr>  
  44.         </table>  
  45.     </form>  
  46. </body>  
  47. </html>  

頁面效果

 

3.2登錄邏輯處理頁面

login_action.jsp

 

[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ page import="java.sql.*" %>    
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8.   <%   
  9.   String username = request.getParameter("username");  
  10.   String password = request.getParameter("password");  
  11.   if(username==null||"".equals(username.trim())||password==null||"".equals(password.trim())){  
  12.       //out.write("用戶名或密碼不能為空!");  
  13.       System.out.println("用戶名或密碼不能為空!");  
  14.       response.sendRedirect("login.jsp");  
  15.       return;  
  16.       //request.getRequestDispatcher("login.jsp").forward(request, response);  
  17.   }  
  18.   boolean isValid = false;  
  19.   Connection con = null;// 創建一個數據庫連接  
  20.   PreparedStatement pre = null;// 創建預編譯語句對象,一般都是用這個而不用Statement  
  21.   ResultSet result = null;// 創建一個結果集對象  
  22.   try  
  23.   {  
  24.       Class.forName("oracle.jdbc.driver.OracleDriver");// 加載Oracle驅動程序  
  25.       //System.out.println("開始嘗試連接數據庫!");  
  26.       String url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:orcl";// 127.0.0.1是本機地址,orcl是Oracle的默認數據庫名  
  27.       String user = "scott";// 用戶名,系統默認的賬戶名  
  28.       String pwd = "tiger";// 你安裝時選設置的密碼  
  29.       con = DriverManager.getConnection(url, user, pwd);// 獲取連接  
  30.      // System.out.println("連接成功!");  
  31.       String sql = "select * from p_user where username=? and password=?";// 預編譯語句,“?”代表參數  
  32.       pre = con.prepareStatement(sql);// 實例化預編譯語句  
  33.       pre.setString(1, username);// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  34.       pre.setString(2, password);// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  35.       result = pre.executeQuery();// 執行查詢,注意括號中不需要再加參數  
  36.       if (result.next()){  
  37.           isValid = true;  
  38.       }  
  39.   }  
  40.   catch (Exception e)  
  41.   {  
  42.       e.printStackTrace();  
  43.   }  
  44.   finally  
  45.   {  
  46.       try  
  47.       {  
  48.           // 逐一將上面的幾個對象關閉,因為不關閉的話會影響性能、並且占用資源  
  49.           // 注意關閉的順序,最后使用的最先關閉  
  50.           if (result != null)  
  51.               result.close();  
  52.           if (pre != null)  
  53.               pre.close();  
  54.           if (con != null)  
  55.               con.close();  
  56.           //System.out.println("數據庫連接已關閉!");  
  57.       }  
  58.       catch (Exception e)  
  59.       {  
  60.           e.printStackTrace();  
  61.       }  
  62.   }  
  63.   if(isValid){  
  64.       System.out.println("登錄成功!");  
  65.       session.setAttribute("username", username);  
  66.       response.sendRedirect("welcome.jsp");  
  67.       return;  
  68.   }else{  
  69.       System.out.println("登錄失敗!");  
  70.       response.sendRedirect("login.jsp");  
  71.       return;  
  72.   }  
  73.   %>  

使用JDBC連接數據庫,如果用戶名或密碼為空時,還是跳轉到登錄頁面login.jsp

 

如果用戶名和密碼不為空,進行連接數據庫查詢用戶表,如果能夠查詢到記錄,表示登錄成功,將用戶信息保存到session,跳轉到歡迎頁面welcome.jsp

如果根據用戶名和密碼查詢不到記錄,表示登錄失敗,重新跳轉到登錄頁面login.jsp

3.3歡迎頁面

welcome.jsp

 

[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'welcom.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     <table>  
  26.         <tr>  
  27.             <td><img src="images/logo4.png" />  
  28.             </td>  
  29.             <td><img src="images/logo2.png" height="90" />  
  30.             </td>  
  31.         </tr>  
  32.         <tr>  
  33.             <td colspan="2"><hr />  
  34.             </td>  
  35.         </tr>  
  36.         <tr>  
  37.             <td>  
  38.                 <table>  
  39.                     <tr>  
  40.                         <td><a>Main</a>  
  41.                         </td>  
  42.                     </tr>  
  43.                     <tr>  
  44.                         <td><a>Menu1</a>  
  45.                         </td>  
  46.                     </tr>  
  47.                     <tr>  
  48.                         <td><a>Menu2</a>  
  49.                         </td>  
  50.                     </tr>  
  51.                     <tr>  
  52.                         <td><a>Menu3</a>  
  53.                         </td>  
  54.                     </tr>  
  55.                     <tr>  
  56.                         <td><a>Menu4</a>  
  57.                         </td>  
  58.                     </tr>  
  59.                     <tr>  
  60.                         <td><a>Menu5</a>  
  61.                         </td>  
  62.                     </tr>  
  63.                     <tr>  
  64.                         <td><a>Menu6</a>  
  65.                         </td>  
  66.                     </tr>  
  67.                     <tr>  
  68.                         <td><a>Menu7</a>  
  69.                         </td>  
  70.                     </tr>  
  71.                     <tr>  
  72.                         <td><a>Menu8</a>  
  73.                         </td>  
  74.                     </tr>  
  75.                 </table></td>  
  76.             <td>  
  77.                 <form action="loginout.jsp" method="post">  
  78.                     <table>  
  79.                         <tr>  
  80.                             <td colspan="2">登錄成功!</td>  
  81.                         </tr>  
  82.                         <tr>  
  83.                             <td>歡迎你,</td>  
  84.                             <td>${username }</td>  
  85.                         </tr>  
  86.                         <tr>  
  87.                             <td colspan="2"><input type="submit" value="退出" /></td>  
  88.                         </tr>  
  89.                     </table>  
  90.                 </form></td>  
  91.         </tr>  
  92.     </table>  
  93. </body>  
  94. </html>  

使用EL表達式展示用戶信息

 

效果

3.4歡迎頁退出邏輯處理頁面

loginout.jsp

 

[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'loginout.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.    <%  
  27.    session.removeAttribute("username");  
  28.    response.sendRedirect("login.jsp");  
  29.    %>  
  30.   </body>  
  31. </html>  

將session的用戶信息移除,跳轉到登錄頁面login.jsp

 

3.5注冊頁面

register.jsp

[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>注冊頁面</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     <form action="register_action.jsp" method="post">  
  26.         <table>  
  27.             <tr>  
  28.                 <td colspan="2">注冊窗口</td>  
  29.             </tr>  
  30.             <tr>  
  31.                 <td>用戶名:</td>  
  32.                 <td><input type="text" name="username" /></td>  
  33.             </tr>  
  34.             <tr>  
  35.                 <td>密碼:</td>  
  36.                 <td><input type="password" name="password1" /></td>  
  37.             </tr>  
  38.             <tr>  
  39.                 <td>確認密碼:</td>  
  40.                 <td><input type="password" name="password2" /></td>  
  41.             </tr>  
  42.             <tr>  
  43.                 <td>email:</td>  
  44.                 <td><input type="text" name="email" /></td>  
  45.             </tr>  
  46.             <tr>  
  47.                 <td colspan="2"><input type="submit" value="注冊" /> <href="login.jsp">返回</a></td>  
  48.             </tr>  
  49.         </table>  
  50.     </form>  
  51. </body>  
  52. </html>  

當在登錄頁面點擊“注冊“時打開用戶注冊頁面

 

效果

3.6注冊邏輯處理頁面

register_action.jsp

[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.sql.*" %>    
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   <%   
  8.   String username = request.getParameter("username");  
  9.   String password1 = request.getParameter("password1");  
  10.   String password2 = request.getParameter("password2");  
  11.   String email = request.getParameter("email");  
  12.   if(username==null||"".equals(username.trim())||password1==null||"".equals(password1.trim())||password2==null||"".equals(password2.trim())||!password1.equals(password2)){  
  13.       //out.write("用戶名或密碼不能為空!");  
  14.       System.out.println("用戶名或密碼不能為空!");  
  15.       response.sendRedirect("register.jsp");  
  16.       return;  
  17.       //request.getRequestDispatcher("login.jsp").forward(request, response);  
  18.   }  
  19.   boolean isValid = false;  
  20.   Connection con = null;// 創建一個數據庫連接  
  21.   PreparedStatement pre = null;// 創建預編譯語句對象,一般都是用這個而不用Statement  
  22.   ResultSet result = null;// 創建一個結果集對象  
  23.   try  
  24.   {  
  25.       Class.forName("oracle.jdbc.driver.OracleDriver");// 加載Oracle驅動程序  
  26.       //System.out.println("開始嘗試連接數據庫!");  
  27.       String url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:orcl";// 127.0.0.1是本機地址,orcl是Oracle的默認數據庫名  
  28.       String user = "scott";// 用戶名,系統默認的賬戶名  
  29.       String pwd = "tiger";// 你安裝時選設置的密碼  
  30.       con = DriverManager.getConnection(url, user, pwd);// 獲取連接  
  31.      //System.out.println("連接成功!");  
  32.       String sql = "select * from p_user where username=?";// 預編譯語句,“?”代表參數  
  33.       pre = con.prepareStatement(sql);// 實例化預編譯語句  
  34.       pre.setString(1, username);// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  35.       result = pre.executeQuery();// 執行查詢,注意括號中不需要再加參數  
  36.       if (!result.next()){  
  37.           sql = "insert into p_user(id,username,password,email) values(?,?,?,?)";// 預編譯語句,“?”代表參數  
  38.           pre = con.prepareStatement(sql);// 實例化預編譯語句  
  39.           pre.setString(1, System.currentTimeMillis()+"");// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  40.           pre.setString(2, username);// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  41.           pre.setString(3, password1);// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  42.           pre.setString(4, email);// 設置參數,前面的1表示參數的索引,而不是表中列名的索引  
  43.           pre.executeUpdate();// 執行  
  44.           isValid = true;  
  45.       }  
  46.   }  
  47.   catch (Exception e)  
  48.   {  
  49.       e.printStackTrace();  
  50.   }  
  51.   finally  
  52.   {  
  53.       try  
  54.       {  
  55.           // 逐一將上面的幾個對象關閉,因為不關閉的話會影響性能、並且占用資源  
  56.           // 注意關閉的順序,最后使用的最先關閉  
  57.           if (result != null)  
  58.               result.close();  
  59.           if (pre != null)  
  60.               pre.close();  
  61.           if (con != null)  
  62.               con.close();  
  63.           //System.out.println("數據庫連接已關閉!");  
  64.       }  
  65.       catch (Exception e)  
  66.       {  
  67.           e.printStackTrace();  
  68.       }  
  69.   }  
  70.   if(isValid){  
  71.       System.out.println("注冊成功,請登錄!");  
  72.       response.sendRedirect("login.jsp");  
  73.       return;  
  74.   }else{  
  75.       System.out.println("用戶名已存在!");  
  76.       response.sendRedirect("register.jsp");  
  77.       return;  
  78.   }  
  79.   %>  

首先判斷用戶名和密碼是否為空,以及密碼和確認密碼是否一致,如果上述條件不成立時,返回到注冊頁面register.jsp

 

如果上述條件成立,就根據用戶名到數據庫查詢,如果能夠查詢到記錄,說明用戶名已經存在,返回到注冊頁面register.jsp

如果查詢不到記錄,說明此用戶名可用來進行注冊,使用JDBC向用戶表 插入1條記錄;之后跳轉到登錄頁面login.jsp

4.總結

本例使用JSP實現用戶登錄,編寫過程中,主要遇到了2個小問題。

一:查詢之后,判斷記錄是否存在,需要使用 if (!result.next()),而不是通常查詢中使用的while循環,這一點需要注意,特別是在處理注冊時

二:關於JSP頁面的編譯報錯問題

當在JSP小腳本中中使用return時要慎重,很可能會出現編譯錯誤

處理方法是,JSP主頁面只使用JSP小腳本,保證return之后沒有還需要編譯的內容即可

以上即為使用JSP實現用戶登錄的簡單介紹,還需要不斷完善。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM