Thursday, April 14, 2011

Chapter 42: Scope of JavaBeans in JSP

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

5 comments:

© 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.

ShareThis

Google+ Followers

Followers