1.Javabean簡介:html
符合某種特定規範的Java類,java
使用的好處:解決代碼重複編寫,功能區分明確,提升代碼維護性。session
2.Javabean設計原則:app
3.Jsp動做元素jsp
爲請求處理階段提供信息。post
動做元素遵循XML元素語法,有一個元素名開始標籤,可有屬性,可選內容,與開始標籤匹配的結束標籤。ui
第一類:存取Javabean有關的:this
<jsp:useBean> <jsp:setProperty> <jsp:getProperty>url
4.JSP頁面使用Javabeans 兩種方法:spa
1.像使用普通Java類同樣,建立Javabean實例。
Users.java
package com.pa; 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; } }
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <%@ page import="com.pa.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"> </head> <body> <% Users user= new Users(); user.setUsername("abc"); user.setPassword("123456"); %> 普通方法應用Javabeans<br> 用戶名 <%= user.getUsername() %> 密 碼 <%= user.getPassword() %> </body> </html>
2.Jsp頁面經過jsp動做標籤使用javabean
<jsp:useBeans>
做用: 在JSP頁面實例化或在指定範圍內使用javabean:
<jsp:useBean id="標識符" class="Java類名" scope="做用範圍" />
<jsp:setProperty>
做用:給已經實例化的javabean對象屬性賦值, 共四種形式
跟表單自動匹配全部屬性
<jsp:setProperty name="Userjavabeanss" property="*" />
跟表單自動匹配指定屬性
<jsp:setProperty name="Userjavabeanss" property="username" />
跟表單無關,經過手工賦值 給屬性
<jsp:setProperty property="username" name="Userjavabeanss" value="石頭"/>
<jsp:setProperty property="password" name="Userjavabeanss" value="1234"/>
跟表單無關,經過URL傳參數賦值 給屬性
//表單<form action="dologin.jsp?urldizhi=888888" />
<jsp:setProperty property="password" name="Userjavabeanss" param="urldizhi" />
<jsp:getProperty>
做用:獲取指定javabean對象的屬性。
<jsp:getProperty property="屬性名" name="JavaBean實例名"/>
3.javabean的四個做用域範圍
說明:使用useBeans的scope屬性能夠用來指定javabean的做用範圍。
page //僅在當前頁面有效
request //可經過HttpRequest.getAttribute()方法獲取javabean對象。
session //可經過HttpSession.getAttribute()方法獲取javabean對象。
application //可經過application.getAttribute()方法獲取javabean對象。
4.階段案例:使用JSP+javabean完成用戶登陸功能
Users.java
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; } }
UserDao.java
package com.dao; import com.po.Users; public class UserDao { public boolean usersLogin(Users u){ if("admin".equals(u.getUsername())&&"123".equals(u.getPassword())){ return true; }else { return false; } } }
login.jsp
<h2>登陸界面</h2> <form action="dologin.jsp" method="post"> <label>用戶名</label> <input type="text" name="username"><br> <label>密  碼</label> <input type="password" name="password" ><br> <input type="submit" value="登陸"> </form>
dologin.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %> <jsp:useBean id="juser" class="com.po.Users" scope="page"/> <jsp:useBean id="UserDao" class="com.dao.UserDao" scope="page"/> <jsp:setProperty property="*" name="juser"/> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; if(UserDao.usersLogin(juser)){ session.setAttribute("zhuanusername", juser.getUsername()) ; request.getRequestDispatcher("login_success.jsp").forward(request, response); }else{ response.sendRedirect("login_failure.jsp"); } %>
粗心:
把表單的用戶名屬性名沒有和 Javabeans中的用戶名屬性一致,致使一直登陸失敗!