A Managed Bean, is a Java class file to which components of JSF pages are mapped. A JSF application has one, or more, Managed Beans that handle the business logic or, a backing Bean for the values of components, displayed on the JSF page. Normally, a Managed Bean has getter and setter methods of the properties mapped as the component's value on JSF pages.
Let's take a simple example to better understand the scope of Managed Beans.
Example
I have created an index.xhtml file with a TextBox and a submit button. The TextBox field value is mapped to the Bean property textname, using the expression language. An action to call an output.xhtml page, on the click event, is defined on the command button. The Output.xhtml file displays the value of the Bean property textname (in other words the value entered into the TextBox).
The following is the code for index.xhtml:
The following is the screenshot of the index.xhtml file when run in a browser:
The following is the code for output.xhtml:
A Java class named ExampleBean.java will act as a Managed Bean for this JSP page.
The following is the code for the ExampleBean.java file:
1. None Scope: A Managed Bean with this scope is not stored anywhere. It is created whenever it is needed.
Let me show you the assigning of the Managed Bean, of a None scope.
Now, when you run the index.xhtml file, enter the value into the TextBox and click on the submit button. You will find it in the Bean, displayed multiple times, in the server output window.
The reason for multiple occurrences, in the Bean output, is the none scope of the Managed Bean. The Managed Bean is initialized each time, it is needed.
2. Request Scope: Under this scope, the Managed Bean value is stored and is available, while the request is alive.
Now, when you run the index.xhtml file, enter the value into the TextBox and click on the submit button. You will find it in the Bean displayed in the server output window and the output.xhtml file displayed in the browser.
Index.xhtml file
Server Output
Output.xhtml
Now, if I return to the index.xhtml file, using the browser's back button, and again enter the value into the TextBox. Click on the submit button and then one more Managed Bean object, will be created.
Server Output
So, each time a request is sent to a Managed Bean, a new object will be created and saved under request scope.
3. Session Scope: Under this scope, the Managed Bean object is saved and is available for use in the entire session. All of the requests, within the same session, share one instance of the Managed Bean.
The following is the updated code for the Managed Bean:
Now, when you run the index.xhtml file, the Bean class object will be created once and saved for the entire session.
Server Output:
Now, if you try and send multiple requests by going to index.xhtml and enter the value and click the submit button, the Bean class object won't be created. There will be output from the setter method only.
Server Output
4. Application Scope: Under this scope, the Managed Bean object is saved and is available for use in the entire application, for all the sessions and requests. A Bean is given application scope, only if we want one instance of the Bean to be shared in the application.
The following is the updated code for the Managed Bean:
Now, when you run the index.xhtml file, the Bean class object will be created once and saved for the entire application.
Server Output:
There are two more scopes for the Managed Bean added to JSF version 2.0: