程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java組件javabean用戶登錄實例詳解

Java組件javabean用戶登錄實例詳解

編輯:關於JAVA

Java組件javabean用戶登錄實例詳解。本站提示廣大學習愛好者:(Java組件javabean用戶登錄實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java組件javabean用戶登錄實例詳解正文


本文簡略講述應用javabean完成用戶登錄,包含用戶登錄,注冊和加入等。

1.關於javabean
JavaBean 是一種JAVA說話寫成的可重用組件。為寫成JavaBean,類必需是詳細的和公共的,而且具有沒有參數的結構器。JavaBean 經由過程供給相符分歧性設計形式的公共辦法將外部域裸露成員屬性,set和get辦法獲得。盡人皆知,屬性稱號相符這類形式,其他Java 類可以經由過程自省機制發明和操作這些JavaBean 的屬性。

2.體系架構
2.1登錄用例圖

2.2頁面流程圖

2.3體系架構圖

2.4數據庫設計
本例應用oracle數據庫

用戶表包含id,用戶名,暗碼,email,共4個字段

-- Create table 
create table P_USER 
( 
 id  VARCHAR2(50) not null, 
 username VARCHAR2(20), 
 password VARCHAR2(20), 
 email VARCHAR2(50) 
) 
tablespace USERS 
 pctfree 10 
 initrans 1 
 maxtrans 255 
 storage 
 ( 
 initial 64 
 minextents 1 
 maxextents unlimited 
 ); 
-- Add comments to the table 
comment on table P_USER 
 is '用戶表'; 
-- Add comments to the columns 
comment on column P_USER.id 
 is 'id'; 
comment on column P_USER.username 
 is '用戶名'; 
comment on column P_USER.password 
 is '暗碼'; 
comment on column P_USER.email 
 is 'email'; 

3.javabean編寫
3.1開辟數據庫底層處置javabean
DBAcess.java

package com.baosight.bean; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
 
/**數據庫操作類 
 * <p>Title:DBAcess </p> 
 * <p>Description:TODO </p> 
 * <p>Company: </p> 
 * @author yuan 
 * @date 2016-5-22 下晝12:40:24*/ 
public class DBAcess { 
 private String driver = "oracle.jdbc.driver.OracleDriver"; 
 private String url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:orcl"; 
 private String username = "scott"; 
 private String password = "tiger"; 
 private Connection conn; 
 private Statement stm; 
 private ResultSet rs; 
 //創立銜接 
 public boolean createConn() { 
  boolean b = false; 
  try { 
   Class.forName(driver);// 加載Oracle驅動法式 
   conn = DriverManager.getConnection(url, username, password); 
   b = true; 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  }// 獲得銜接 
  catch (ClassNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  return b; 
 } 
 //修正 
 public boolean update(String sql){ 
  boolean b = false; 
  try { 
   stm = conn.createStatement(); 
   stm.execute(sql); 
   b = true; 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  return b; 
 } 
 //查詢 
 public void query(String sql){ 
  try { 
   stm = conn.createStatement(); 
   rs = stm.executeQuery(sql); 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
 } 
 //斷定有沒有數據 
 public boolean next(){ 
  boolean b = false; 
  try { 
   if(rs.next()){ 
    b = true; 
   } 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  return b; 
 } 
 //獲得表字段值 
 public String getValue(String field) { 
  String value = null; 
  try { 
   if (rs != null) { 
    value = rs.getString(field); 
   } 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  return value; 
 } 
 //封閉銜接 
 public void closeConn() { 
  try { 
   if (conn != null) { 
    conn.close(); 
   } 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
 } 
 //封閉statement 
 public void closeStm() { 
  try { 
   if (stm != null) { 
    stm.close(); 
   } 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
 } 
 //封閉ResultSet 
 public void closeRs() { 
  try { 
   if (rs != null) { 
    rs.close(); 
   } 
  } catch (SQLException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
 } 
 public String getDriver() { 
  return driver; 
 } 
 public void setDriver(String driver) { 
  this.driver = driver; 
 } 
 public String getUrl() { 
  return url; 
 } 
 public void setUrl(String url) { 
  this.url = url; 
 } 
 public String getUsername() { 
  return username; 
 } 
 public void setUsername(String username) { 
  this.username = username; 
 } 
 public String getPassword() { 
  return password; 
 } 
 public void setPassword(String password) { 
  this.password = password; 
 } 
 public Statement getStm() { 
  return stm; 
 } 
 public void setStm(Statement stm) { 
  this.stm = stm; 
 } 
 public ResultSet getRs() { 
  return rs; 
 } 
 public void setRs(ResultSet rs) { 
  this.rs = rs; 
 } 
 public void setConn(Connection conn) { 
  this.conn = conn; 
 } 
 public Connection getConn() { 
  return conn; 
 } 
} 

上述數據庫操作類應用JDBC銜接數據庫,並封裝了銜接數據庫、查詢、修正、封閉資本等辦法。

3.2開辟JavaBean營業邏輯組件
UserBean.java

package com.baosight.bean; 
 
import com.baosight.util.GenerateUUID; 
 
/** 
 * <p>Title:UserBean </p> 
 * <p>Description:TODO </p> 
 * <p>Company: </p> 
 * @author yuan 
 * @date 2016-5-22 下晝1:05:00*/ 
public class UserBean { 
 //登錄驗證 
 public boolean valid(String username,String password){ 
  boolean isValid = false; 
  DBAcess db = new DBAcess(); 
  if(db.createConn()){ 
   String sql = "select * from p_user where username='"+username+"' and password='"+password+"'"; 
   db.query(sql); 
   if(db.next()){ 
    isValid = true; 
   } 
   db.closeRs(); 
   db.closeStm(); 
   db.closeConn(); 
  } 
  return isValid; 
 } 
 //注冊驗證 
 public boolean isExist(String username){ 
  boolean isValid = false; 
  DBAcess db = new DBAcess(); 
  if(db.createConn()){ 
   String sql = "select * from p_user where username='"+username+"'"; 
   db.query(sql); 
   if(db.next()){ 
    isValid = true; 
   } 
   db.closeRs(); 
   db.closeStm(); 
   db.closeConn(); 
  } 
  return isValid; 
 } 
 //注冊用戶 
 public boolean add(String username,String password,String email){ 
  boolean isValid = false; 
  DBAcess db = new DBAcess(); 
  if(db.createConn()){ 
   String sql = "insert into p_user(id,username,password,email) values('"+GenerateUUID.next()+"','"+username+"','"+password+"','"+email+"')"; 
   isValid = db.update(sql); 
   db.closeStm(); 
   db.closeConn(); 
  } 
  return isValid; 
 } 
} 

上述營業邏輯javabean,界說了登錄驗證、注冊驗證和新增用戶等辦法

3.3關於生成獨一ID
下面在新增用戶時須要拔出ID,本例應用UUID來生成獨一ID

GenerateUUID.java

package com.baosight.util; 
 
import java.util.Date; 
 
/** 
 * <p>Title:GenerateUUID </p> 
 * <p>Description:TODO </p> 
 * <p>Company: </p> 
 * @author yuan 
 * @date 2016-5-22 下晝1:31:46*/ 
public class GenerateUUID { 
  private static Date date = new Date(); 
// private static StringBuilder buf = new StringBuilder(); 
  private static int seq = 0; 
  private static final int ROTATION = 99999; 
  public static synchronized long next(){ 
  if (seq > ROTATION) seq = 0; 
//  buf.delete(0, buf.length()); 
  date.setTime(System.currentTimeMillis()); 
  String str = String.format("%1$tY%1$tm%1$td%1$tk%1$tM%1$tS%2$05d", date, seq++); 
  return Long.parseLong(str); 
 } 
  public static void main(String[] args) { 
   for(int i=0;i<100;i++){ 
    System.out.println(next()); 
   } 
 } 
} 

4.頁面編寫
4.1登錄頁面
login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
  
 <title>登錄頁面</title> 
  
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
 </head> 
 
 <body> 
 <form action="login_action.jsp" method="post"> 
  <table> 
   <tr> 
    <td colspan="2">登錄窗口</td> 
   </tr> 
   <tr> 
    <td>用戶名:</td> 
    <td><input type="text" name="username" /> 
    </td> 
   </tr> 
   <tr> 
    <td>暗碼:</td> 
    <td><input type="text" name="password" /> 
    </td> 
   </tr> 
   <tr> 
    <td colspan="2"><input type="submit" value="登錄" /> <a href="register.jsp">注冊</a> 
    </td> 
   </tr> 
  </table> 
 </form> 
</body> 
</html> 

頁面後果


4.2登錄營業邏輯處置頁面
login_action.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<%@ page import="java.sql.*" %> 
<%@ page import="com.baosight.bean.*" %> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
 <% 
 String username = request.getParameter("username"); 
 String password = request.getParameter("password"); 
 if(username==null||"".equals(username.trim())||password==null||"".equals(password.trim())){ 
  //out.write("用戶名或暗碼不克不及為空!"); 
  System.out.println("用戶名或暗碼不克不及為空!"); 
  response.sendRedirect("login.jsp"); 
  return; 
  //request.getRequestDispatcher("login.jsp").forward(request, response); 
 } 
 UserBean userBean = new UserBean(); 
 boolean isValid = userBean.valid(username,password); 
 if(isValid){ 
  System.out.println("登錄勝利!"); 
  session.setAttribute("username", username); 
  response.sendRedirect("welcome.jsp"); 
  return; 
 }else{ 
  System.out.println("用戶名或暗碼毛病,登錄掉敗!"); 
  response.sendRedirect("login.jsp"); 
  return; 
 } 
 %> 

下面的JSP挪用了Javabean停止營業處置
當用戶名或暗碼為空時前往登錄頁面login.jsp

當登錄勝利後將用戶信息保留到session,跳轉到迎接頁面welcome.jsp

當登錄掉敗時前往登錄頁面login.jsp

4.3登錄勝利迎接頁面
welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
  
 <title>My JSP 'welcom.jsp' starting page</title> 
  
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
 </head> 
 
 <body> 
 <table> 
  <tr> 
   <td><img src="images/logo4.png" /> 
   </td> 
   <td><img src="images/logo2.png" height="90" /> 
   </td> 
  </tr> 
  <tr> 
   <td colspan="2"><hr /> 
   </td> 
  </tr> 
  <tr> 
   <td> 
    <table> 
     <tr> 
      <td><a>Main</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu1</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu2</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu3</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu4</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu5</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu6</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu7</a> 
      </td> 
     </tr> 
     <tr> 
      <td><a>Menu8</a> 
      </td> 
     </tr> 
    </table></td> 
   <td> 
    <form action="loginout.jsp" method="post"> 
     <table> 
      <tr> 
       <td colspan="2">登錄勝利!</td> 
      </tr> 
      <tr> 
       <td>迎接你,</td> 
       <td>${username }</td> 
      </tr> 
      <tr> 
       <td colspan="2"><input type="submit" value="加入" /></td> 
      </tr> 
     </table> 
    </form></td> 
  </tr> 
 </table> 
</body> 
</html> 

頁面後果

4.4加入登錄營業處置頁面
loginout.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
  
 <title>My JSP 'loginout.jsp' starting page</title> 
  
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
 
 </head> 
 
 <body> 
 <% 
 session.removeAttribute("username"); 
 response.sendRedirect("login.jsp"); 
 %> 
 </body> 
</html> 

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

4.5用戶注冊頁面
register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
  
 <title>注冊頁面</title> 
  
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
 </head> 
 
 <body> 
 <form action="register_action.jsp" method="post"> 
  <table> 
   <tr> 
    <td colspan="2">注冊窗口</td> 
   </tr> 
   <tr> 
    <td>用戶名:</td> 
    <td><input type="text" name="username" /></td> 
   </tr> 
   <tr> 
    <td>暗碼:</td> 
    <td><input type="text" name="password1" /></td> 
   </tr> 
   <tr> 
    <td>確認暗碼:</td> 
    <td><input type="text" name="password2" /></td> 
   </tr> 
   <tr> 
    <td>email:</td> 
    <td><input type="text" name="email" /></td> 
   </tr> 
   <tr> 
    <td colspan="2"><input type="submit" value="注冊" /> <a href="login.jsp">前往</a></td> 
   </tr> 
  </table> 
 </form> 
</body> 
</html> 

運轉後果

4.6注冊營業處置頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ page import="java.sql.*" %> 
<%@ page import="com.baosight.bean.*" %> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
 
 <% 
 String username = request.getParameter("username"); 
 String password1 = request.getParameter("password1"); 
 String password2 = request.getParameter("password2"); 
 String email = request.getParameter("email"); 
 if(username==null||"".equals(username.trim())||password1==null||"".equals(password1.trim())||password2==null||"".equals(password2.trim())||!password1.equals(password2)){ 
  //out.write("用戶名或暗碼不克不及為空!"); 
  System.out.println("用戶名或暗碼不克不及為空!"); 
  response.sendRedirect("register.jsp"); 
  return; 
  //request.getRequestDispatcher("login.jsp").forward(request, response); 
 } 
 UserBean userBean = new UserBean(); 
 boolean isExit = userBean.isExist(username); 
 if(!isExit){ 
  userBean.add(username, password1, email); 
  System.out.println("注冊勝利,請登錄!"); 
  response.sendRedirect("login.jsp"); 
  return; 
 }else{ 
  System.out.println("用戶名已存在!"); 
  response.sendRedirect("register.jsp"); 
  return; 
 } 
 %> 

下面的JSP挪用了Javabean停止營業處置

當用戶名或暗碼為空時前往注冊頁面register.jsp

當注冊用戶名在數據庫不存在時,新增用戶

新增勝利後跳轉到登錄頁面login.jsp

當注冊用戶名在數據庫存在時,前往注冊頁面register.jsp

5.總結

本例應用javabean對數據庫操作和營業邏輯處置停止了封裝。

以上即為應用javabean完成用戶登錄的簡略引見,還須要赓續完美,願望年夜家一路進修提高!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved