付费节点推荐
免费节点
节点使用教程
Model1结构:
javabean的出现可以使jsp页面中使用javabean封装的数据或者调用javabean的业务逻辑代码,这样大大提升了程序的可维护性。
使用Model1思想完成用户登录功能(jsp+javabean)
分为:
- User.java (用户类)
- UserDao.java (处理业务逻辑,判断用户登录)
- login.jsp (登录页面)
- dologin.jsp (处理登录页面)
- login_failure.jsp (登录失败页面)
- login_success.jsp (登录成功页面)
项目资源:http://pan.baidu.com/s/1o6DsKfK
[t]User.java[/t]
package com.po;
public class User {
private String username;
private String passwd;
public User(){}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
[t]UserDao.java[/t]
package com.dao;
//用户业务逻辑
import com.po.User;
public class UserDao {
public boolean usersLogin(User u){
if("admin".equals(u.getUsername())&&"admin".equals(u.getUsername()))
{
return true;
}
else
{
return false;
}
}
}
[t]login.jsp[/t]
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- Page title -->
<title>bcoder.cn Login</title>
<!-- End of Page title -->
<!-- Libraries -->
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/easyTooltip.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<!-- End of Libraries -->
</head>
<body>
<div id="container">
<div class="logo">
<a href="#"><img src="assets/logo.png" alt="" /></a>
</div>
<div id="box">
<form action="dologin.jsp" method="post">
<p class="main">
<label>用户名: </label>
<input name="username" value="" />
<label>密码: </label>
<input type="password" name="password" value="">
</p>
<p class="space">
<input type="submit" value="登录" class="login" style="cursor: pointer;"/>
</p>
</form>
</div>
</div>
</body>
</html>
[t]dologin.jsp[/t]
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<jsp:useBean id="loginUser" class="com.po.User" scope="page" />
<jsp:useBean id="userDao" class="com.dao.UserDao" scope="page" />
<jsp:setProperty property="*" name="loginUser" />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//如果用户和密码都等于admin,则登录成功
if(userDao.usersLogin(loginUser))
{
session.setAttribute("loginUser", loginUser.getUsername());
request.getRequestDispatcher("login_success.jsp").forward(request, response);
}
else
{
response.sendRedirect("login_failure.jsp");
}
%>
[t]login_sucess.jsp[/t]
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- Page title -->
<title>imooc - Login</title>
<!-- End of Page title -->
<!-- Libraries -->
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/easyTooltip.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<!-- End of Libraries -->
</head>
<body>
<div id="container">
<div class="logo">
<a href="#"><img src="assets/logo.png" alt="" /></a>
</div>
<div id="box">
<%
String loginUser = "";
if(session.getAttribute("loginUser")!=null)
{
loginUser = session.getAttribute("loginUser").toString();
}
%>
欢迎您<font color="red"><%=loginUser%></font>,登录成功!
</div>
</div>
</body>
</html>
[t]login_failure.jsp[/t]
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- Page title -->
<title>imooc - Login</title>
<!-- End of Page title -->
<!-- Libraries -->
<link type="text/css" href="css/login.css" rel="stylesheet" />
<link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/easyTooltip.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<!-- End of Libraries -->
</head>
<body>
<div id="container">
<div class="logo">
<a href="#"><img src="assets/logo.png" alt="" /></a>
</div>
<div id="box">
登录失败!请检查用户或者密码!<br>
<a href="login.jsp">返回登录</a>
</div>
</div>
</body>
</html>
未经允许不得转载:Bcoder资源网 » 使用jsp+javabean完成用户登录功能
评论前必须登录!
登陆 注册