四大属性范围内的javabean_第1页
四大属性范围内的javabean_第2页
四大属性范围内的javabean_第3页
四大属性范围内的javabean_第4页
四大属性范围内的javabean_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Javabean保存范围<jsp:useBean>指令上存在一个scope属性,表示一个javabean的保存属性,保存的范围一共有以下4种。1、 page:保存在一夜的范围中,跳转后此javabean无效。2、 request:一个javabean对象可以保存在一次服务器跳转的范围内。3、 session:在一个用户的操作中保存,重新打开浏览器时才会声明新的javabean。4、 application:在整个服务器上保存,服务器关闭时才会消失。下面编写一个count.java类,以分别测试4种属性范围。package org.xml.yang.cai;public class

2、countprivate int count=0;public person()System.out.println("*实例化对象的产生*");public int math()return +count;page范围内的javabean<% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>response对象</title><

3、/head><body><jsp:useBean id="ps" scope="page" class="org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4></body></html>执行结果为:访问1次Tomcat后台:每刷新一次页面,其都会增加一个输入:Request范围内的javabean<% page contentType="text/html;charset=GBK

4、"%><% page pageEncoding="GBK"%><html><head><title>response对象</title></head><%request.setCharacterEncoding("GBK"); %><body><jsp:useBean id="ps" scope="request" class="org.xml.yang.cai.person"/

5、><h4>访问<%=ps.math()%>次</h4><jsp:forward page="4.jsp"/></body></html><% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>response对象</title></head><

6、body><jsp:useBean id="ps" scope="request" class="org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4></body></html>执行结果为:访问2次Tomcat后台:每刷新一次页面,其都会增加一个输入:session范围内的javabean<% page contentType="text/html;charset=GBK"%>

7、;<% page pageEncoding="GBK"%><html><head><title>response对象</title></head><%request.setCharacterEncoding("GBK"); %><body><jsp:useBean id="ps" scope="session" class="org.xml.yang.cai.person"/><h4

8、>访问<%=ps.math()%>次</h4></body></html>刷新页面23次后,执行结果为:访问23次Tomcat后台:不管刷新多少次,其只能输出一个:但是重启浏览器或重启服务器后:访问1次application范围内的javabean<% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>respons

9、e对象</title></head><%request.setCharacterEncoding("GBK"); %><body><jsp:useBean id="ps" scope="application" class="org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4></body></html>刷新页面23次后,执行结果为:访问23次T

10、omcat后台:不管刷新多少次,其只能输出一个:但是重启浏览器或重启服务器后:访问24次Javabean的删除如果一个javabean不再使用的话,则可以直接使用4种属性范围的removeAttribute()方法进行删除。删除page范围的javabean可以使用pageContext.removeAttribute(实例化对象(id);删除request范围的javabean可以使用request.removeAttribute(实例化对象(id);删除session范围的javabean可以使用session.removeAttribute(实例化对象(id);删除application

11、范围的javabean可以使用application.removeAttribute(实例化对象(id);例如以下代码1所示:<% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>response对象</title></head><%request.setCharacterEncoding("GBK"); %>&

12、lt;body><jsp:useBean id="ps" scope="page" class="org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4><%pageContext.removeAttribute("ps");%></body></html>执行结果为:访问1次Tomcat后台:每刷新一次页面,其都会增加一个输入:例如以下代码2所示:<% page cont

13、entType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>response对象</title></head><%request.setCharacterEncoding("GBK"); %><body><jsp:useBean id="ps" scope="request" class=&

14、quot;org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4><jsp:forward page="4.jsp"/></body></html><% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>respon

15、se对象</title></head><body><jsp:useBean id="ps" scope="request" class="org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4><%request.removeAttribute("ps");%></body></html>执行结果为:访问2次Tomcat后台:每刷新一次页面,其都会增

16、加一个输入:例如以下代码3所示:<% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>response对象</title></head><%request.setCharacterEncoding("GBK"); %><body><jsp:useBean id="ps" sc

17、ope="session" class="org.xml.yang.cai.person"/><h4>访问<%=ps.math()%>次</h4><%session.removeAttribute("ps");%></body></html>执行结果为:访问1次Tomcat后台:每刷新一次页面,其都会增加一个输入:例如以下代码4所示:<% page contentType="text/html;charset=GBK"%><% page pageEncoding="GBK"%><html><head><title>response对象</title></head><%request.setCharacterEncoding("GBK"); %><bo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论