• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about bean creating

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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);
}
...
search{
...
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.
 
Saloon Keeper
Posts: 27886
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
reply
    Bookmark Topic Watch Topic
  • New Topic