Java Bean Scopes in JSF

By: Ivan Lim  

When you configure a JavaBean to be used in a JSF page, you can configure it with one of four
scopes:

• None: Objects with this scope are not visible in any JSF page. When used in the configuration file, they indicate managed beans that are used by other managed beans in the application. Objects with none scope can use other objects with none scope.

• Request: Objects with this scope are visible from the start of the request until the end
of the request. Request scope starts at the beginning of a request and ends when the
response has been sent to the client. If the request is forwarded, the objects are visible
in the forwarded page, because that page is still part of the same request/response
cycle. Objects with request scope can use other objects with none, request, session,
or application scope.

• Session: An object with session scope is visible for any request/response cycle that
belongs to a session. Objects with this scope have their state persisted between
requests and last until the object or the session is invalidated. Objects with session
scope can use other objects with none, session, or application scope.

• Application: An object with application scope is visible in all request/response cycles
for all clients using the application, for as long as the application is active. Objects with
application scope can use other objects with none or application scope.

You may know that when you use the <useBean> standard action, you can specify similar scopes for the JavaBean. The difference is that JSP scope includes an additional scope named page. Because JSF requests often involve navigation between pages, objects with page scope have no value in a JSF application.

Most often, you will define your managed beans to have session scope. However, in some cases, you may have a managed bean that encapsulates global data. For example, you may have a managed bean that holds information common to every page in the application; in that case, you would define the managed bean to have application scope. Managed beans that you use only within a single request/response will have request scope.




Archived Comments

1. what about object bean with view scope ??
View Tutorial          By: abdelaziz akharraz at 2012-10-09 15:06:52

2. What about object bean with view scope?
View Tutorial          By: Awo at 2012-02-11 12:17:36

3. tnx man that was short and well explained...
View Tutorial          By: jsf noob at 2012-02-02 13:24:12

4. Thank uvery much :) u r so helpful
View Tutorial          By: Ahmed at 2011-12-04 10:01:11

5. if you put some examples that will be great !!
View Tutorial          By: Hari at 2011-09-02 16:27:03

6. sanx. for ur explanation that was good enough
View Tutorial          By: Ahmed Nazmey at 2011-08-20 09:23:31

7. nice example it's enough for jsf beginner
View Tutorial          By: lakshmanan at 2011-08-01 03:46:30

8. Thanks. Well explanation..
View Tutorial          By: Ebi Divakar at 2011-07-26 04:31:30

9. What I understand from the above explanation is that a bean in application scope is available as lon
View Tutorial          By: Abhilash at 2010-06-09 04:29:39

10. thanks. that was fair enough. :)
View Tutorial          By: Mustafa at 2010-05-16 10:12:33


Most Viewed Articles (in JSF )

Latest Articles (in JSF)

Comment on this tutorial