目录
javabean简介
符合某种特定规范的Java类
优点:
1.简化代码
2.功能区分明确
3.提高可维护性
javabean设计原则
jsp动作
普通方法创建javabean
//usesbean.jsp
<%@ 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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
<h1>使用useBean动作创建javabean的实例</h1>
<hr>
用户名:<%=Users.getUsername() %><br>
密码:<%=Users.getPassword() %><br>
</body>
</html>
//index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="com.po.Users" %>
<%
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 'index.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>
<%
Users user = new Users();
user.setUsername("admin"); //设置用户名
user.setPassword("123456");//设置密码
%>
<h1>使用普通方式创建javabean的实例</h1>
<hr>
用户名:<%=user.getUsername() %><br>
密码:<%=user.getPassword() %><br>
</body>
</html>
jsp:useBean
作用:在jsp页面中实例化或者在制定范围内使用javabean:
相对于上面的usebean.jsp jsp实例化的名字改为了myUser,所以下面的用户名密码夜莺改变
<h1>使用useBean动作创建javabean的实例</h1>
<hr>
//使用了
用户名:<%=myUsers.getUsername() %><br>
密码:<%=myUsers.getPassword() %><br>
</body>
</html>
jsp:setProperty
根据表单自动匹配所有的属性
//dologin.jsp
<body>
<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
//
<!--根据表单自动匹配所有的属性 -->
<%--
<jsp:setProperty name="myUsers" property="*"/>
--%>
//name应该和javabean中保持一致
<!-- 使用getProperty方式来获取用户名和密码 -->
用户名:<jsp:getProperty name="myUsers" property="username"/> <br>
密码:<jsp:getProperty name="myUsers" property="password"/><br>
根据表单自动匹配部分的属性
<!--根据表单匹配所有部分的属性 -->
<%--
<jsp:setProperty name="myUsers" property="username"/>
--%>
根表单无关,通过手工赋值给属性
<!--根表单无关,通过手工赋值给属性 -->
<%--
<jsp:setProperty name="myUsers" property="username" value="lisi"/>
<jsp:setProperty name="myUsers" property="password" value="888888"/>
--%>
<!--username 通过自动匹配赋值 -->
<jsp:setProperty name="myUsers" property="username"/>
<!--password通过URL传参数给属性赋值 -->
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
jsp:getProperty
<!-- 使用getProperty方式来获取用户名和密码 -->
用户名:<jsp:getProperty name="myUsers" property="username"/> <br>
密码:<jsp:getProperty name="myUsers" property="password"/><br>
javabean的四个 作用域范围
<!--使用动作元素获取用户名和密码-->
<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
用户名:<jsp:getProperty name="myUsers" property="username"/><br>
密码:<jsp:getProperty name="myUsers" property="password"/><br>
<!-- 使用内置对象获取用户名和密码 -->
<hr>
用户名:<%=((Users)application.getAttribute("myUsers")).getUsername()%><br>
密码:<%=((Users)application.getAttribute("myUsers")).getPassword() %><br>
分别使用session application request获取
<%--
用户名:<%=((Users)application.getAttribute("myUsers")).getUsername()%><br>
密码:<%=((Users)application.getAttribute("myUsers")).getPassword() %><br>
--%>
<%--
用户名:<%=((Users)session.getAttribute("myUsers")).getUsername()%><br>
密码:<%=((Users)session.getAttribute("myUsers")).getPassword() %><br>
--%>
<%--
用户名:<%=((Users)request.getAttribute("myUsers")).getUsername()%><br>
密码:<%=((Users)request.getAttribute("myUsers")).getPassword() %><br>
--%>
Model1
Model1 实现用户登录
package com.dao;
import com.po.Users;
//用户的业务逻辑类,用来封装业务逻辑
public class UsersDAO {
//用户登录方法
public boolean usersLogin(Users u)
{
if("admin".equals(u.getUsername())&&"admin".equals(u.getPassword()))
{
return true;
}
else
{
return false;
}
}
}
package com.po;
//用户类,封装数据的
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;
}
}
//dologin.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="loginUser" class="com.po.Users" scope="page"/>
<jsp:useBean id="userDAO" class="com.dao.UsersDAO" scope="page"/>
<jsp:setProperty property="*" name="loginUser"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//如果用户和密码都等于admin,则登录成功
//直接调用业务逻辑userDAO,直接判断
if(userDAO.usersLogin(loginUser))
{
session.setAttribute("loginUser", loginUser.getUsername());
request.getRequestDispatcher("login_success.jsp").forward(request, response);
}
else
{
response.sendRedirect("login_failure.jsp");
}
%>