來源:尤膩膩小廚房 發(fā)布時間:2018-11-24 10:20:24 閱讀量:1296
javaEE之jsp+JavaBean實現(xiàn)登錄功能(不加數(shù)據(jù)庫)
實現(xiàn)效果
JavaBean文件(全部放在nmx包中,請注意更改):
#UserBean.java
package nmx;
public class UserBean {
private String username;
private String realname;
private String sex;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
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
#UserDataBean.java
package nmx;
public class UserDataBean {
private String username;
private String password;
private String realname;
private String sex;
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 String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public UserDataBean(String u,String p,String r,String s){
username=u;
password=p;
realname=r;
sex=s;
}
public UserDataBean(){}
//默認(rèn)的無參的構(gòu)造函數(shù)
}
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
#UserLoginBean
package nmx;
public class UserLoginBean {
private String username;
private String password;
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#UserManagerBean
package nmx;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class UserManagerBean {
private List<UserDataBean> userList;
public List<UserDataBean> getUserList() {
return userList;
}
public void setUserList(List<UserDataBean> userList) {
this.userList = userList;
}
public UserManagerBean(){
userList=new LinkedList<UserDataBean>();
userList.add(new UserDataBean("sa","sa","管理員","男"));
userList.add(new UserDataBean("test","abc123","測試員","女"));
}
public UserBean login(String username,String password){//登錄驗證函數(shù)
UserBean userBean=null;
Iterator<UserDataBean> iter=userList.iterator();
while(iter.hasNext()){
UserDataBean user=iter.next();
if(user.getUsername().equals(username)){
if(user.getPassword().equals(password)){
userBean =new UserBean();
userBean.setUsername(username);
userBean.setRealname(user.getRealname());
userBean.setSex(user.getSex());
}
break;
}
}
return userBean;
}
}
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
jsp/html文件:
#login.html
<!DOCTYPE html>
<html>
<head>
<title>登錄</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="validate.jsp" method="post">
用戶名:<input type="text" name="username"/><br>
密碼: <input type="password" name="password" /><br>
<input type="submit" value="登錄" />
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#validate.jsp
<%@ page language="java" import="java.util.*,nmx.*" 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 'validate.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>
<% request.setCharacterEncoding("utf-8"); %>
<!-- 將登錄表單里的參數(shù)讀到UserLoginBean的java類里面 -->
<jsp:useBean id="userLogin" class="nmx.UserLoginBean" scope="request"/>
<jsp:setProperty property="*" name="userLogin" />
<jsp:useBean id="userManager" class="nmx.UserManagerBean" scope="application" />
<% UserBean userBean=userManager.login(userLogin.getUsername(), userLogin.getPassword());
if(userBean!=null){
session.setAttribute("user", userBean);
response.sendRedirect("main.jsp");
} else{
response.sendRedirect("error.html");
}
%>
</body>
</html>
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
#main.jsp
<%@ page language="java" import="java.util.*,nmx.*" 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 'main.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>
<jsp:useBean id="user" class="nmx.UserBean" scope="session" />
<% if(user.getUsername()==null) response.sendRedirect("login.html"); %>
Hi,<%=user.getUsername() %><br>
<jsp:useBean id="userManager" class="nmx.UserManagerBean" scope="application" />
<table border="1">
<caption>用戶列表</caption>
<tr><th>用戶名</th><th>密碼</th><th>姓名</th><th>性別</th></tr>
<% Iterator<UserDataBean> iter=userManager.getUserList().iterator();
while(iter.hasNext()){
UserDataBean userData=iter.next();
%>
<tr><td><%=userData.getUsername() %></td><td><%=userData.getPassword() %></td>
<td><%=userData.getRealname() %></td><td><%=userData.getSex() %></td></tr>
<% } %>
</table>
</body>
</html>
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
#error.html
<!DOCTYPE html>
<html>
<head>
<title>error.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="text/html;charest=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
用戶名或密碼錯誤,請<a href="login.html">重新登錄</a>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
完成后將此web項目部署到Tomcat 8 上,啟動服務(wù)器,通過相應(yīng)連接即可訪問驗證。
---------------------