JSP复习整理(五)JavaBean生命周期

本文介绍了JavaBean的创建及不同作用域(page、request、session 和 application)下的生命周期示例,展示了计数器在各作用域内的表现。

一、创建一个JavaBean

UserBean.java

package jsp.test;

public class UserBean {

    private String userName;
    private String pwd;
    private String name;
    private String gender;
    private int age;
    private String email;
    private String tel;
    private String mphone;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getMphone() {
        return mphone;
    }
    public void setMphone(String mphone) {
        this.mphone = mphone;
    }
    
    
    
}
View Code

 


二、JavaBean的生命周期

        ---------1.page范围

counter.java

package bean;

public class counter {
     public counter(){
     }
     private int count=0;
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        count = count;
    }
     
}
View Code

 

usingCounter.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>page 类型的生命周期</title>
</head>
<body>
<jsp:useBean id="count" scope="page" class="bean.counter"/>
<font color="blue">Show:page</font><br><br>
<br>You are the 
<font color=green>
     <jsp:getProperty name="count" property="count"/></font>Viewer
</body>
</html>
View Code

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

        -----------2.request

 setRequest.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>request type life time</title>
</head>
<body>
   <jsp:useBean id="count" scope="request" class="bean.counter"/>
   <font color=blue>area: request </font><br><br>
   You are the <font color=green>
   <jsp:getProperty property="count" name="count"/>s
   </font>viewer<br><br>
   
   <jsp:forward page="request.jsp"></jsp:forward>


</body>
</html>
View Code

 

requset.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>request type life time</title>
</head>
<body>
    <jsp:useBean id="count" scope="request" class="bean.counter"/>
    <font color=blue>request.jsp</font><br><br><br>
    You are the <font color=green>
    <jsp:getProperty name="count" property="count"/>
    </font>viewer<br>
    <%
        out.println("This is request.jsp page's counter.......");
    %>
</body>
</html>
View Code

计数器会加1。。

    3------------session

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>session type life time</title>
</head>
<body>
  <jsp:useBean id="count" scope="session" class="bean.counter"></jsp:useBean>
  <font color=blue>area: session</font><br><br><br>
      You are the :
          <font color="green">
           <jsp:getProperty property="count" name="count"/>viewer
            </font>
</body>
</html>
View Code

session每次浏览计数都会从头开始。。

  4------------application

appication.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>application type life time</title>
</head>
<body>

<jsp:useBean id="count" scope="application" class="bean.counter"></jsp:useBean>
  <font color=blue>area: session</font><br><br><br>
      You are the :
          <font color="green">
           <jsp:getProperty property="count" name="count"/>viewer
            </font>

</body>
</html>
View Code

 

四个中生命周期最长的一个,计数器会不断累加,除非删除,与jsp引擎相当

转载于:https://www.cnblogs.com/haixiaomei/p/O0941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值