managed bean scopes in jsf

hi,

CAn we use managed bean scope always as application to avoid confusion. Are there any security issue or resources or memory issue by increasing highest application all the time.

Please advise.

Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
You can, but why would you need to do that? What confusion does it create if you use different scopes?
0
SrinivasDandeCommented:
Hi ,
You should not use application scope as managed bean scope Always.
For Example , Consider the login  requirement .
User is submiting username and password from the browser and you have the managed bean called UserBean with username and Password variables.
like
public class UserBean{
private String Username;
private String password;

...

}
When user submits username and password , that data will be stored in UserBean intance. When specify the UserBean scope as application then that UserBean can be accessed by all the users.which is very dangerous.
So You should not do that.
You analize the requirement and specify the scope.
0
gudii9Author Commented:
>>When specify the UserBean scope as application then that UserBean can be accessed by all the users


How the user in UI(say JSP or XHML) will know about the managed bean scope which is in the model portion of web MVC right?

Please advise with some simple practical code example??
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

girionisCommented:
The web MVC includes JSP files. All the files in the application have access to beans.
0
gudii9Author Commented:
I still not clear difference between request and session scope.

Let us say I gave request scope for managed bean.


LEt us say in the login.jsp 'user1' loged in with his password say 'password1'. Managed bean username and password getters, setters cpatured those 'user1', 'password1' and stored to database

Now another user say 'user2' logged in from some other computer  through login.jsp with say password 'password2'.

Since we gave managed bean scope as request managed bean cannot get 'user2' and 'password2' and insert to Database.

(If it would have been session scope then managed bean get 'user2' and 'password2' and insert to Database.???)

Please advise
0
girionisCommented:
Since we gave managed bean scope as request managed bean cannot get 'user2' and 'password2' and insert to Database.
It can if the login and the insertion in the database are part of the same request. If you want to do the login first, and then on a second request, add the credentials to the database then you need another scope like session.
0
gudii9Author Commented:
>>>LEt us say in the login.jsp 'user1' loged in with his password say 'password1'. Managed bean username and password getters, setters cpatured those 'user1', 'password1' and stored to database

>>>Now another user say 'user2' logged in from some other computer  through login.jsp with say password 'password2'.


Above two scenarios comes under two separate requests right. What is the liternal meaning of request, session, application used in web applciation?
Please advise

>>> If you want to do the login first, and then on a second request, add the credentials to the database then you need another scope like session.


How do you create another request for insertion logic separate from login logic . please advise
0
girionisCommented:
I think you better have a look at this link, it explains all scopes.
0
gudii9Author Commented:
>>For a web application to use a bean that injects another bean class, the bean needs to be able to hold state over the duration of the user’s interaction with the application. The way to define this state is to give the bean a scope



>>>the bean needs to be able to hold state over the duration of the user’s interaction with the application

Let us say there are two beans BeanA and BeanB

I have to unserstand as below right


For a web application to use a BeanA that injects another bean class BeanB, the BeanA needs to be able to hold state over the duration of the user’s interaction with the application(or BeanB???). The way to define this state is to give the bean a scope



Please advise
0
gudii9Author Commented:
Request
 @RequestScoped
 A user’s interaction with a web application in a single HTTP request.
 
Session
 @SessionScoped
 A user’s interaction with a web application across multiple HTTP requests.
 
Application
 @ApplicationScoped
 Shared state across all users’ interactions with a web application.
 
Dependent
 @Dependent
 The default scope if none is specified; it means that an object exists to serve exactly one client (bean) and has the same lifecycle as that client (bean).
 
Conversation
 @ConversationScoped
 A user’s interaction with a JavaServer Faces application, within explicit developer-controlled boundaries that extend the scope across multiple invocations of the JavaServer Faces lifecycle. All long-running conversations are scoped to a particular HTTP servlet session and may not cross session boundaries.




I see they are using client(bean) interchangebly.  Does client means the user who logs in. Please advise
0
girionisCommented:
It means the user who issues the request(s). Not necessarily the user who logs in.
0
gudii9Author Commented:
>>Session
 @SessionScoped
 A user’s interaction with a web application across multiple HTTP requests.




>>>>It means the user who issues the request(s). Not necessarily the user who logs in.


can a user interacts multiple HTTP requests without loging in. I wonder how is it possible. please advis with practical scenario example
0
girionisCommented:
I explained in one of your previous questions that the session is created the first time the user accesses the site, so no need to log in.

An example would be a site like amazon. You go there, you browse and you put books in the shopping cart without logging in. Amazon knows what items you put in the shopping cart because of the session.
0
gudii9Author Commented:
That makes more sense. In the amazon example how do i visualize

>>A user’s interaction with a web application across multiple HTTP requests.



How a user makes multiple HTTP requests while purchasing let us say 10 different java, j2ee books.

To me it looks like everything one request. please advise
0
girionisCommented:
How a user makes multiple HTTP requests while purchasing let us say 10 different java, j2ee books.

Each time you click on a link, photo, button, etc, is a different request.
0
gudii9Author Commented:
once user logs in to say amazon site he can click multiple links/photo/buttons in that 'user' login scope right which might create  several 'requests scopes' for each click right. so one user scope can consist of several request scopes within that. please advise
0
gudii9Author Commented:
>>>When user submits username and password , that data will be stored in UserBean intance. When specify the UserBean scope as application then that UserBean can be accessed by all the users.which is very dangerous.


is the data stored in user bean instance or through user bean instance in the database?


if it is stored through user bean instance in the database then what is the point of scope. please advise
0
gudii9Author Commented:
Please advise
0
girionisCommented:
so one user scope can consist of several request scopes within that. please advise

In this context you are right. A session scope can consist of several request scopes.
0
gudii9Author Commented:
Using Scopes
For a web application to use a bean that injects another bean class, the bean needs to be able to hold state over the duration of the user’s interaction with the application. The way to define this state is to give the bean a scope. You can give an object any of the scopes described in Table 28-1, depending on how you are using it.

IS scope is for jsp or for the bean or or for one bean to get access on other beanor  for the web MVC application as a whole or for the user who is sending request without logging in or user who logs in.

I am not still able to grasp this concept very clearly. Is there is any alternate link or ebook or book to understand this concept precisely.

Dependent
 @Dependent
 The default scope if none is specified; it means that an object exists to serve exactly one client (bean) and has the same lifecycle as that client (bean).
 
Conversation
 @ConversationScoped
 A user’s interaction with a JavaServer Faces application, within explicit developer-controlled boundaries that extend the scope across multiple invocations of the JavaServer Faces lifecycle. All long-running conversations are scoped to a particular HTTP servlet session and may not cross session boundaries.



I was not able to clearly understood above two scopes. CAn you please elaborate. are they newly introduced scopes. i only heard till now request, session, application
0
girionisCommented:
IS scope is for jsp or for the bean or or for one bean to get access on other beanor  for the web MVC application as a whole or for the user who is sending request without logging in or user who logs in.

Scope is used for everything. When you say "a bean in a request scope" it means this bean is only available for this request. WHen you say "a variable in a request scope" it means that the variable is only available for this request, and so on.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.