定義一個JavaBean(JavaBean實際上就是一個簡單的java類)
javac -d ./ UserBean.java
生成包文件
package com.javaweb.ch07; //一個簡單的JavaBean示例 public class UserBean{ //用戶名屬性 private String username; //用戶密碼屬性 private String password; //獲取用戶名 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; } }
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>調用JavaBean</title> </head> <body> <%--通過JavaBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean> <% //設置user的username屬性 user.setUsername("zhangdapeng"); //設置user的password屬性 user.setPassword("zhangda890126;;"); //打印輸出user的username out.println("用戶名為:"+user.getUsername()+"<br />"); //打印輸出user的password out.println("用戶的密碼為:"+user.getPassword()+"<br />"); %> <% try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅動程序 }catch(ClassNotFoundException e){ out.println("找不到驅動類");//拋出異常時,提示信息 } %> </body> </html>
生成的類一定要放在自己項目的目錄下WEB-INF中的classes文件夾下面,沒有的話自己創建。
設置javaBean的屬性:
jsp中使用<jsp:setProperty>動作指令來設置JavaBean屬性,有四種格式
<jsp:setProperty name="實例化對象名" property="*" />
這中方式中的"*"表示根據表單傳的所有參數來設置JavaBean屬性,其傳過來的參數值必須與JavaBean中的屬性名稱保持一致
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>用戶表單</title> </head> <body> <form action="SetPropertyDemo.jsp" method="post"> <table> <tr><td colspan="2">用戶表單</td></tr> <tr><td>用戶名:</td><td><input type="text" name="username" /></td></tr> <tr><td>用戶密碼:</td><td><input type="password" name="password" /></td></tr> <tr><td colspan="2"><input type="submit"><input type="reset" /></td></tr> <tr></tr> </table> </form> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置JavaBean屬性</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="*" /> <% //打印輸出user的username out.println("用戶名為:"+user.getUsername()+"<br />"); //打印輸出user的password out.println("用戶的密碼為:"+user.getPassword()+"<br />"); %> </body> </html> <jsp:setProperty name="實例化對象名" property="屬性名稱" />
提交表單的頁面同上
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置JavaBean屬性</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" /> <% //打印輸出user的username out.println("用戶名為:"+user.getUsername()+"<br />"); //打印輸出user的password out.println("用戶的密碼為:"+user.getPassword()+"<br />"); %> </body> </html> <jsp:setProperty name="實例化對象名" property="屬性名稱" param="參數名稱" /> <%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>用戶表單</title> </head> <body> <form action="SetPropertyDemo.jsp" method="post"> <table> <tr><td colspan="2">用戶表單</td></tr> <tr><td>用戶名:</td><td><input type="text" name="username" /></td></tr> <tr><td>用戶密碼:</td><td><input type="password" name="userpassword" /></td></tr> <tr><td colspan="2"><input type="submit"><input type="reset" /></td></tr> <tr></tr> </table> </form> </body> </html>
這裡注意password表單的name屬性
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置JavaBean屬性</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" param="username"/> <span style="color:#e53333;"><jsp:setProperty name="user" property="<span style="color:#003399;">password</span>" param="<span style="color:#003399;">userpassword</span>"/></span> <% //打印輸出user的username out.println("用戶名為:"+user.getUsername()+"<br />"); //打印輸出user的password out.println("用戶的密碼為:"+user.getPassword()+"<br />"); %> </body> </html>
上面的代碼注意紅色中的藍色部分,這個彈性更好
<jsp:setProperty name="實例化對象名" property="屬性名稱" value="屬性值" /> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置JavaBean屬性</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" value="Devidpeng"/> <jsp:setProperty name="user" property="password" value="Devidpeng"/> <% //打印輸出user的username out.println("用戶名為:"+user.getUsername()+"<br />"); //打印輸出user的password out.println("用戶的密碼為:"+user.getPassword()+"<br />"); %> </body> </html>
獲得JavaBean的屬性:
<jsp:getProperty name="" property=""/> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>獲取JavaBean屬性</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="page" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" value="Devidpeng"/> <jsp:setProperty name="user" property="password" value="zhangdapeng"/> <jsp:getProperty name="user" property="username" /> <jsp:getProperty name="user" property="password" /> </body> </html>
設置JavaBean的范圍:
設置page范圍的JavaBean
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="<span style="color:#e53333;">page</span>" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" value="page_username"/> <jsp:setProperty name="user" property="password" value="page_password"/> <jsp:forward page="PageJavaBeanDemo2.jsp"/> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="<span style="color:#e53333;">page</span>" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:getProperty name="user" property="username" /> <jsp:getProperty name="user" property="password" /> </body> </html>
設置request范圍的JavaBean
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="<b><span style="color:#e53333;">request</span></b>" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" value="request_username"/> <jsp:setProperty name="user" property="password" value="request_password"/> <jsp:forward page="PageJavaBeanDemo2.jsp"/> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="<span style="color:#e53333;">request</span>" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:getProperty name="user" property="username" /> <jsp:getProperty name="user" property="password" /> </body> </html>
設置session范圍的JavaBean
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="session" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" value="session_username"/> <jsp:setProperty name="user" property="password" value="session_password"/> <a href="PageJavaBeanDemo2.jsp">跳轉到PageJavaBeanDemo2.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="session" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:getProperty name="user" property="username" /> <jsp:getProperty name="user" property="password" /> </body> </html>
設置application范圍的JavaBean
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="application" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:setProperty name="user" property="username" value="application_username"/> <jsp:setProperty name="user" property="password" value="application_password"/> <a href="PageJavaBeanDemo2.jsp">跳轉到PageJavaBeanDemo2.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="application" class="com.javaweb.ch07.UserBean"></jsp:useBean> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:getProperty name="user" property="username" /> <jsp:getProperty name="user" property="password" /> </body> </html>
移除JavaBean的屬性:
JavaBean的移除根據不同的范圍而不同
page范圍,request范圍,session范圍,application范圍對應的使用pageContext,request,session,application
方法都使用removeAttribute()方法
示例
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>設置page的范圍</title> </head> <body> <%--通過useBean動作指令調用JavaBean--%> <jsp:useBean id="user" scope="application" class="com.javaweb.ch07.UserBean"></jsp:useBean> <% //移除application范圍的屬性 application.removeAttribute("user"); %> <%--根據所有的參數設置JavaBean中屬性--%> <jsp:getProperty name="user" property="username" /> <jsp:getProperty name="user" property="password" /> </body> </html>