In the previous few chapters, we have seen what JavaBeans are and how to use them inside JSP Pages. As with any object, there is a scope, i.e., the places where the object is visible. In this chapter, we are going to take a look at the scope of the JavaBeans that are declared inside a JSP.
So, lets get started!!!
JavaBean Scope in a JSP
JavaBeans have scope, just like all variables. You declare this in the jsp:useBean element when you use the bean for the first time. There are multiple scopes possible for a bean.
They are:
1. Page
2. Request
3. Session
4. Application
We have started with Page which is the least visible and went on till Application which is most Visible. To make it easier to understand, take a look at the picture below. It shows you how the scopes are arranged.
Bean objects are only available/accessible in the scope they were declared/created to be visible.
Let us look at these scopes in a little more detail.
Page Scope
If a bean is declared with Page scope, it is equivalent to local variables in regular java code. If a bean is declared with page scope, the reference to the bean disappears once the JSP page is processed. It cannot be referenced by any other JSP or servlet, even if a forward or include is used.
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="page" / >
Request Scope
If a bean is declared with Request scope, it can be accessed by any JSP or Servlet within the same request. The reference remains alive in any other servlet or JSP that is called by jsp:include and jsp:forward or using the RequestDispatcher object. You can declare a bean in Request scope as follows:
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="request" / >
Session Scope
If a bean is declared with Session scope, it is available to all the JSP & Servlets that are accessed by a single user. Even if the user navigates across multiple pages, the beans are available for use. You can declare a bean with session scope as follows:
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="session" / >
Application Scope
Application scope is the widest or most visible scope. A bean declared with application scope is visible to just about any servlet or JSP in the whole applications context, across user sessions. Though, this might sound awesome, we usually do not declare beans with this scope because it can end up affecting us instead of being beneficial. For ex: if the value entered by one user in his web page is visible to another guy, it would be disastrous, wouldn’t it? So, we need to be cautious before using this scope. We can declare a bean with application scope as follows:
Ex:
< jsp : useBean id="address" class="com.test.AddressBean" scope="application" / >
Previous Chapter: Chapter 41 - Modifying Bean Properties
Next Chapter: Chapter 43 - Accessing JavaBeans in JSP
Thursday, April 14, 2011
Chapter 42: Scope of JavaBeans in JSP
Labels:
application scope,
bean scope,
java bean scope,
javabean scope,
jsp and java beans,
page scope,
request scope,
session scope,
using java beans in jsp
| Reactions: |
Subscribe to:
Post Comments (Atom)
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.
Popular Posts
-
The following are some questions that you might encounter when you face an Interview for a position of a Senior Java/J2EE Developer. Design ...
-
The following are some questions you might encounter with respect to Java Multi-threading in any Interview. Multi-threading is a powerful an...
-
The following are some questions you might encounter with respect to Java Server Pages or JSPs in any Interview. JSPs are an integral part o...
-
Access Modifiers are one of the most important topics when it comes to taking the SCJP exam. You can expect a number of questions from these...
-
Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the a...
-
1. Does Hibernate implement its functionality using a minimal number of database queries to ensure optimal output? Hibernate can make cert...
-
The last thing we need to look at in our series of chapters on threads, is how threads can interact with one another to communicate about, a...
-
You might be wondering, is Stack and Heap such a large topic that I have dedicated one full chapter to it? Actually it isnt such a big topic...
-
In the chapter on Inner classes, I had promised that we will look in more detail about the different types of inner classes. Well here we ar...
-
This chapter is probably going to be the most important or rather most confusing chapter from the exam perspective. You can expect atleast a...


Very helpful topic.
ReplyDeleteThanks
vry nice...
ReplyDeletethanke u so much
ReplyDelete