File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Question about bean creating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Question about bean creating " Watch "Question about bean creating " New topic

Question about bean creating

Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68

I'm quite new with JSF and while creating my first page I came up to a situation when I had to initialize the JAXBcontex. This usually takes some seconds and should be done only once, at the start of the application. I began reading about initializing in jsf and run into some solutions concerning creating another servlet that would be launched on the startup. Although the solution seemed correct for my desire it was as well quite vague and hard for me to comprehend. First I wanted to check how exactly the proces of creating beans looks like. I though that with each request send to the servlet (f.e. pressing a button) a new bean is created. So I've made a simple test to see how it works using the random generator. I have created 3 variables and changed them in different places of the code. The first two were in the constructor of the bean and the third was in a function called by pressed button.

public Bean()
randomGenerator = new Random();
randomInt2 = randomGenerator.nextInt(100);
if (jc == null)
m = new HlResults();
jc = m.getJAXBContext();
randomInt1 = randomGenerator.nextInt(100);
randomInt3 = randomGenerator.nextInt(100);

To my big surprise with each press of the button only the third variable changed. Even closing and opening the window resulted in the same values for the first two variables. This was even more puzzling since I set the bean for session scope.
Could any one please explain it in a noob-firendly way? :> Is it possible that if I would create the JAXB context the same way as above it would be done only once for the session? If I'm wrong please give me some advice how to manage it properly.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

In JSF, there is the concept of managed beans. Managed beans are simply beans that are constructed by the JSF framework instead of by explicit program logic. To make that happen, you declare them - plus any initialization information for them - in the faces-config file.

One of the things you declare for a managed bean is its scope. Scope is actually a basic JSP bean concept and comes in 4 flavors: application, session, request, and page, but page scope is not meaningful in basic JSF, due to the way it operates. It sounds like you want an application-scope bean, but the default, I believe, is request scope. In request scope, the bean is created and destroyed each time you send a request to the server (NOT when you enter and leave a page, which may entail multiple requests).

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Question about bean creating
It's not a secret anymore!