来源:尤腻腻小厨房 发布时间:2018-11-24 10:20:24 阅读量:1221
javaEE之jsp+JavaBean实现登录功能(不加数据库)
实现效果
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(){}
//默认的无参的构造函数
}
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){//登录验证函数
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"); %>
<!-- 将登录表单里的参数读到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 上,启动服务器,通过相应连接即可访问验证。
---------------------