首页 > 代码库 > JSP复习整理(五)JavaBean

JSP复习整理(五)JavaBean

一、创建一个JavaBean

UserBean.java

技术分享
 1 package jsp.test; 2  3 public class UserBean { 4  5     private String userName; 6     private String pwd; 7     private String name; 8     private String gender; 9     private int age;10     private String email;11     private String tel;12     private String mphone;13     public String getUserName() {14         return userName;15     }16     public void setUserName(String userName) {17         this.userName = userName;18     }19     public String getPwd() {20         return pwd;21     }22     public void setPwd(String pwd) {23         this.pwd = pwd;24     }25     public String getName() {26         return name;27     }28     public void setName(String name) {29         this.name = name;30     }31     public String getGender() {32         return gender;33     }34     public void setGender(String gender) {35         this.gender = gender;36     }37     public int getAge() {38         return age;39     }40     public void setAge(int age) {41         this.age = age;42     }43     public String getEmail() {44         return email;45     }46     public void setEmail(String email) {47         this.email = email;48     }49     public String getTel() {50         return tel;51     }52     public void setTel(String tel) {53         this.tel = tel;54     }55     public String getMphone() {56         return mphone;57     }58     public void setMphone(String mphone) {59         this.mphone = mphone;60     }61     62     63     64 }
View Code


二、JavaBean的生命周期---------1.page范围

Counter.java

技术分享
 1 package bean; 2  3 public class Counter { 4      public Counter(){ 5      } 6      private int count=0; 7     public int getCount() { 8         return count; 9     }10     public void setCount(int count) {11         count = count;12     }13      14 }
View Code

usingCounter.jsp

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>page 类型的生命周期</title> 8 </head> 9 <body>10 <jsp:useBean id="count" scope="page" class="bean.Counter"/>11 <font color="blue">Show:page</font><br><br>12 <br>You are the 13 <font color=green>14      <jsp:getProperty name="count" property="counter"/></font>Viewer15 </body>16 </html>
View Code

使用page计数器的值永远保持为1,不更新。。

三、JavaBean的生命周期-----------2.request

 

JSP复习整理(五)JavaBean