2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Find the suitable bean scope in JSF

In my project JSF 2.2 is being used and I mean to say there are 4 different beans are available.like,

@SessionScoped
@RequestScoped
@ApplicationScoped
@ViewScoped

Basically i want to know the purpose of each bean scope so that can finalize the perfect scope according to my project requirement.

userimage
As of JSF 2.x there are 4 Bean Scopes:

        @SessionScoped
        @RequestScoped
        @ApplicationScoped
        @ViewScoped


Session Scope: The session scope persists from the time that a session is established until session termination. A session terminates if the web application invokes the invalidate method on the HttpSession object, or if it times out.

RequestScope: The request scope is short-lived. It starts when an HTTP request is submitted and ends after the response is sent back to the client. If you place a managed bean into request scope, a new instance is created with each request. It is worth considering request scope if you are concerned about the cost of session scope storage.

ApplicationScope:
The application scope persists for the entire duration of the web application. That scope is shared among all requests and all sessions. You place managed beans into the application scope if a single bean should be shared among all instances of a web application. The bean is constructed when it is first requested by any user of the application, and it stays alive until the web application is removed from the application server.

ViewScope:
View scope was added in JSF 2.0. A bean in view scope persists while the same JSF page is redisplayed. (The JSF specification uses the term view for a JSF page.) As soon as the user navigates to a different page, the bean goes out of scope.

I hope it will be easy for you to finalize the best bean for your requirement.

Answer is