It's not a secret anymore!
The moose likes JSF and the fly likes Calling a backing bean method with no user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Calling a backing bean method with no user input" Watch "Calling a backing bean method with no user input" New topic

Calling a backing bean method with no user input

scott gaidos

Joined: Jan 30, 2002
Posts: 6
I have a session scope backing bean that I want to use for security authentication and authorization.

How can I invoke the methods on the bean from a JSP with no user interaction?
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
All session scoped backing bean resides in the session of course. You can directly retrieve these beans as you normally do with session attributes, provided you know the id of the backing bean.

SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="" target="_blank" rel="nofollow"></a>
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
In case you decide you want to do it inside of a backing bean:

I beleive that's how you do it... there might be some mistakes but that's the general idea.
Sergey Smirnov
Ranch Hand

Joined: May 29, 2003
Posts: 167
Just be careful with getting a reference to the managed bean from the session map directly. It might not be there yet. Managed Bean has a lazy initialization. It means the session scope managed bean is initialized and put in the session bean not at the moment the session is created, but when the managed bean is first time referenced.

This following code returns the reference to the managed bean and create it if it is not in the session map yet:

Sergey :
[ March 19, 2006: Message edited by: Sergey Smirnov ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Actually, by definition, a session-scoped object is ultimately the result of a user interaction. Where and how, of course, is the question.

Now if you want something to exist "before" anyone interacts, you'd use appliaction scope, instead.

Note that "before" may be an extremely short period of time, depending on who lazily the app initiailizes its contexts.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Calling a backing bean method with no user input
jQuery in Action, 3rd edition