JSP+JavaBean實現用戶登錄功能


整個項目邏輯非常簡單,項目含有Users,UserDao兩個JavaBean,
登入login.jsp頁面,將表單信息傳給doLogin.jsp信息處理頁面,判斷登錄信息是否正確,登入信息正確,跳轉到login_success.jsp頁面,否則跳轉到login_failure.jsp頁面
代碼如下:

/**
*登錄用戶Bean
*/

public class Users {
private String username;///用戶名
private String password;//密碼

public Users(){}

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;
}


}
/*
*UserDao用戶邏輯處理
*/

package Dao;

import com.po.Users;

public class UserDao {
//判斷登錄結果
public boolean userLogin(Users user){
if("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())){
return true;
}else{
return false;
}
}
}
/*
*登錄頁面login.jsp
*/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login頁面</title>
</head>
<body>
<form action="userbean.jsp" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" value="" name="username"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
/*
*處理登錄信息doLogin.jsp
*/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<jsp:useBean id="loginUser" class="com.po.Users" scope="page"></jsp:useBean>
<jsp:useBean id="userDao" class="Dao.UserDao"></jsp:useBean>
<jsp:setProperty property="*" name="loginUser"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用userbean創建實例</title>
</head>
<body>
<%
if(userDao.userLogin(loginUser)){
session.setAttribute("username", loginUser.getUsername());
//登錄成功,跳轉到顯示成功信息界面
request.getRequestDispatcher("login_success.jsp").forward(request, response);
}else{
//登錄失敗,跳轉到顯示錯誤信息界面
response.sendRedirect("login_failure.jsp");
}
%>

</body>
</html>
/*
*登錄成功頁面login_sucsses.jsp
*/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄成功</title>
</head>
<body>
<h1>登錄成功!!!</h1>
用戶<font color="red"><%=session.getAttribute("username") %></font>歡迎您!<br>

</body>
</html>
/*
*登錄失敗頁面login_failure.jsp
*/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄失敗</title>
</head>
<body>
<h1><font color="red">登錄失敗,請檢查用戶名或密碼!!!</font></h1>
<a href="login.jsp">重新登錄</a>
</body>
</html>

項目究竟實現登錄功能沒???這就要大伙們自己動手實驗了=.=別偷懶!


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com