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