激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

香港云服务器
服務器之家 - 編程語言 - JAVA教程 - Java組件javabean用戶登錄實例詳解

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

2020-05-04 14:06flab100 JAVA教程

這篇文章主要為大家詳細介紹了Java組件javabean用戶登錄實例,內容有用戶登錄,注冊和退出等,感興趣的小伙伴們可以參考一下

本文簡單講述使用javabean實現用戶登錄,包括用戶登錄,注冊和退出等。

1.關于javabean
JavaBean 是一種JAVA語言寫成的可重用組件。為寫成JavaBean,類必須是具體的和公共的,并且具有無參數的構造器。JavaBean 通過提供符合一致性設計模式的公共方法將內部域暴露成員屬性,set和get方法獲取。眾所周知,屬性名稱符合這種模式,其他Java 類可以通過自省機制發現和操作這些JavaBean 的屬性。

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

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

2.2頁面流程圖

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

2.3系統架構圖

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

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

用戶表包括id,用戶名,密碼,email,共4個字段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<%@ 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>

頁面效果

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

4.2登錄業務邏輯處理頁面
login_action.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<%@ 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>

頁面效果

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

4.4退出登錄業務處理頁面
loginout.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<%@ 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>

運行效果

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

4.6注冊業務處理頁面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ 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實現用戶登錄的簡單介紹,還需要不斷完善,希望大家一起學習進步!

延伸 · 閱讀

精彩推薦
585
主站蜘蛛池模板: 夜夜b| 青久草视频 | 天天夜夜操操 | 91精品国产777在线观看 | gril hd| 99riav国产在线观看 | 欧美一级黄视频 | 秋霞a级毛片在线看 | 久久久久久久午夜 | 亚洲精久 | 宅男噜噜噜66国产免费观看 | 久久99精品久久久久久园产越南 | 久久成人免费观看 | 91国内精品久久久久免费影院 | 国产宾馆3p国语对白 | 亚洲一区二区免费 | 免费永久看羞羞片网站入口 | 免费在线看黄 | 亚洲国产一区二区三区 | 国产精品二区高清在线 | wwwxxx国产 | 一级大黄毛片 | 538在线精品 | 欧美四级在线观看 | 免费黄色小视频网站 | 中文字幕精品在线播放 | 欧美高清一级片 | av噜噜在线 | 欧美黄色大片免费观看 | 久久精品中文字幕一区二区三区 | 伊人在线视频 | 久久久麻豆| 最近日本电影hd免费观看 | xxxx69hd一hd72 | 国产一区二区欧美 | 日本欧美在线播放 | 高清av免费 | 免看一级片 | 色999国产| 欧美成人性色 | av在线1|