一、创建一个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;
    }

}

二、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;
    }

}

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>

使用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>

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>

计数器会加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>

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>

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

JSP复习整理(五)JavaBean生命周期的更多相关文章

  1. SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。

    熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...

  2. 面试之jsp、Servlet相关知识——生命周期, 区别等

    1.servlet生命周期 所谓生命周期,指的是servlet容器如何创建servlet实例.分配其资源.调用其方法.并销毁其实例的整个过程. 阶段一: 实例化(就是创建servlet对象,调用构造器 ...

  3. JSP基础知识➣结构及生命周期(一)

    概述 网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面.容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP ...

  4. Android学习整理之Activity生命周期篇

    一.Activity生命周期说明   Activity的四种状态: ⒈活动状态(Active or Running):也称为运行状态,处于Activity栈顶,在用户界面中最上层,完全能被用户看到,能 ...

  5. vue学习(五)生命周期 的钩子函数

    生命周期的钩子函数 主要有以下几种 beforeCreate created beforeMount mounted beforeUpdate updated activated deactivate ...

  6. JSP复习整理(五)JavaBean使用表单处理数据

    一.先建立用户输入的数据 usingGetparameter.html <!DOCTYPE html> <html> <head> <meta charset ...

  7. JSP复习整理(一)表单

    好久没更了,一周完成了SRDP,一周完成了课程设计,这一周就要好好回顾回顾Java Web的学习轨迹了. 用的eclipse Mars 一.表单 start.jsp <%@ page langu ...

  8. JSP复习整理(二)基本语法

    最基础的整理.. 一.语句声明 <%@ page language="java" contentType="text/html; charset=UTF-8&quo ...

  9. JSP复习整理(四)Cookie

    一.useCookie.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

随机推荐

  1. Gc.Db之循序渐进

    距离上次写Gc.Db框架已经有一段时间了,最近默默对框架代码已经做了不少优化和功能,且已经提交至nuget,大家如果想使用此框架,可以通过nuget搜索:Gc.Db进行下载和安装包. 本篇文章主要是介 ...

  2. Android基础总结(十)

    内容提供者(掌握) 应用的数据库是不允许其他应用访问的 内容提供者的作用就是让别的应用访问到你的私有数据 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数 ...

  3. vue-router

    官方文档: 旧版:https://github.com/vuejs/vue-router/tree/1.0/docs/zh-cn 新版:http://router.vuejs.org/(2.0版本) ...

  4. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...

  5. [Android] Visual Studio Emulator For Android 相关

    1.修改设备名 C:\Users\[用户名]\AppData\Local\Microsoft\VisualStudioEmulator\Android\Containers\Local\Devices ...

  6. theano sparse_block_dot

    theano 中的一个函数 sparse_block_dot; Function: for b in range(batch_size): for j in range(o.shape[1]): fo ...

  7. xpath 学习一: 节点

    xpath 中,有七种类型的节点: 元素.属性.文本.命名空间.处理指令.注释.以及根节点 树的根成为文档节点或者根节点. 节点关系: Parent, Children, sibling(同胞), A ...

  8. Echarts 饼图标题文字换行问题

    var option = { title : { text: '数据来源', x:'center' }, tooltip : { trigger: 'item', formatter: "{ ...

  9. 2MyBatis入门--深入浅出MyBatis技术原理与实践(笔记)

    什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  10. [慢查优化]建索引时注意字段选择性 & 范围查询注意组合索引的字段顺序

    文章转自:http://www.cnblogs.com/zhengyun_ustc/p/slowquery2.html 写在前面的话: 之前曾说过"不要求每个人一定理解 联表查询(join/ ...