Programming Tutorials

Java Bean Scopes in JSF

By: Ivan Lim in JSF Tutorials on 2007-09-18  

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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSF )

Latest Articles (in JSF)