chapter 91 scopes of variables and javabeans javaserver pages by xue bai
TRANSCRIPT
Chapter 9 2
Objectives
In this chapter, you will:
• Learn the difference between variable scope and variable duration
• Learn the difference between variables defined in declaration and variables defined in scriptlets
• Create and use page scope, session scope, and application scope variables
• Create and use page scope, request scope, session scope, and application scope beans
Chapter 9 3
Variable Duration
• Variable duration is the period during
which the variable exists in memory
• Some variables exist briefly, some are
repeatedly created and destroyed, and
others exist after your Web application
starts
Chapter 9 4
Variable Scope
• The scope of a variable refers to where a variable can be referenced in your JSP scripts
• Some variables can be referenced from any JSP pages in your Web application, some can be referenced within the same session, and others can be referenced from only limited portions of a JSP page
• Duration and scope are two different attributes of variables
• A variable may still exist in memory even if it is not referenced in a program
Chapter 9 5
Page Scope Variables
• Variables defined in either declaration or scriptlets
• They can be referenced only on the page where they are defined
• Variables defined in scriptlets are created and destroyed every time the page is requested
• Variables defined in declaration are created once when the page is requested the first time
Chapter 9 6
Session Scope Variables
• Objects that are bound to the session object
• The same lifetime as the session object
• They are created when a new session starts, and destroyed when a session expires
• Accessible in the current session only
Chapter 9 7
Application Scope Variables
• The variables are bound to application
objects
• Accessible to all clients
• The same lifetime as the application object
Chapter 9 8
JavaBean Scope
<jsp:useBean id=”bean_name” class=”class_name” scope=”bean_scope”/>
• A bean object can have one of the following scopes:
– page scope
– request scope
– session scope
– application scope
Chapter 9 9
Page Scope Beans
Default Scope
• Created when the page on which the bean resides is requested and destroyed when the page is finished
• Accessible from the page only
<jsp:useBean id=”bean_name” class=”class_name” scope=”page”/>
Chapter 9 10
Request Scope Bean
• The same lifetime as the request object
• Accessible within the same request
• Without forwarding, the request scope is the same as the page scope
<jsp:useBean id=”bean_name” class=”class_name” scope=”request”/>
Chapter 9 11
Session Scope Bean
• The same lifetime as session object
• Accessible from all JSP pages within the
current session
<jsp:useBean id=”bean_name” class=”class_name”
scope=”session”/>
Chapter 9 12
Instantiation of Session Bean
<jsp:useBean id="beanName” scope=”session”>
<jsp:setProperty name="myName"
property="someProperty" />
</jsp:useBean>
• Then the setProperty action is executed
only once upon the bean is instantiated
Chapter 9 16
Application Scope Beans
• The same lifetime as the application object
• Shared by all clients
<jsp:useBean id="counterBean"
class="com.jspbook.chapter09.Counter" scope="application"/>
• If a session scope bean has the same name as an application scope bean, then the application scope bean is “hidden” from the point where the session scope bean is referenced