Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 91 Scopes of Variables and JavaBeans JavaServer Pages By Xue Bai.

Similar presentations


Presentation on theme: "Chapter 91 Scopes of Variables and JavaBeans JavaServer Pages By Xue Bai."— Presentation transcript:

1 Chapter 91 Scopes of Variables and JavaBeans JavaServer Pages By Xue Bai

2 Chapter 92 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

3 Chapter 93 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

4 Chapter 94 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

5 Chapter 95 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

6 Chapter 96 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

7 Chapter 97 Application Scope Variables The variables are bound to application objects Accessible to all clients The same lifetime as the application object

8 Chapter 98 JavaBean Scope A bean object can have one of the following scopes: –page scope –request scope –session scope –application scope

9 Chapter 99 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

10 Chapter 910 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

11 Chapter 911 Session Scope Bean The same lifetime as session object Accessible from all JSP pages within the current session

12 Chapter 912 Instantiation of Session Bean Then the setProperty action is executed only once upon the bean is instantiated

13 Chapter 913 Using the Shopping Cart

14 Chapter 914 Adding Items to the Shopping Cart

15 Chapter 915 Checking Out

16 Chapter 916 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

17 Chapter 917 Application Scope Bean

18 Chapter 918 Session Scope Bean

19 Chapter 919 The Session Scope Bean Hides the Application Scope Bean


Download ppt "Chapter 91 Scopes of Variables and JavaBeans JavaServer Pages By Xue Bai."

Similar presentations


Ads by Google