Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Where i can put a System.out.println("alert") to know when Bean in destroyed?  RSS feed

 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Since I have still a lot to learn, i always use System.out.println("something") in the code to understand its flow...for example i put System.out.println("Bean created") to the bean constructor to better follow the bean lifecycle.
Where i can write System.out.println("Bean created") to know where it's destroyed?
thanks
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For web applications, we recommend using loggers instead of System.out. For one thing, you can never tell where a webapp server will send the output, including il inferno.

However, there are 2 distinct events that could be considered as "destruction" here.

1. When the bean goes out of scope. Once that happens, the bean still occupies resources, but it can no longer be located, so it effectively no longer exists.

2. When the JVM Garbage Collector scavenges the bean, breaking it down into its low-level components and releasing them.

In practical terms, the one you normally worry about is going out of scope, but the rules are quite clear there, since that's the very definition of the scope.

A) Request-scope beans go out of scope at the instant that the response output stream is posted back to the client.

B) Session-scope beans go out of scope when the session is invalidated - you can create a session listener, but the usual cause of invalidation is logging out

C) View-scope beans are effectively session-scope beans, but they go out of scope when you transfer to a different View.

D) Application-scope beans go out of scope when the application terminates.

The JSP page scope does not apply to JSF.
 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer, I still have a doubt....
A very simple scenario could be this one:

index.xhtml



page2.xhtml



model.java



Well, I Have noted that the bean is first created when I access to the page (during the rendering, ok) but than it is acreated again again after i click on the commundButton, even though in the page02 there isn't any bean reference, and so it should not be a request...why?
 
Brendan Healey
Ranch Hand
Posts: 218
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I can't remember if this works with JSF managed beans (I use CDI beans and it does) but try this:



Edit: just checked and it looks like there may be some issues with view scoped beans but as you are
using request scoped it might work: http://stackoverflow.com/questions/6368840/predestroy-never-called-on-viewscoped
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request scope bean is created, referenced, and destroyed when the page is initially displayed

When you click the commandButton, a new request is submitted, so a new request scope bean is created and posted with valued from the form (if there are any). Since you're navigating to a different View, these values will be destroyed unread along with the request scope bean.
 
massimo tarantelli
Ranch Hand
Posts: 35
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request scope bean is created, referenced, and destroyed when the page is initially displayed

Does It mean that the bean is destroyed after the page rendering? (always speaking about request beans)
Or it'll be destroyed when a new request occurs?
thanks
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, the bean isn't "destroyed". It just goes out of scope. The garbage collector will handle the actual destruction. But "destroyed" is a close enough approximation for what happens.

The Request-scope bean is created on-demand when a request comes in. It is used to process the request and as the reference model for rendering the response. Once the response has been rendered - which is to say that the outgoing HTML has been created and passed on to the netword, the Request-scope bean is immediately discarded. It does not endure waiting for a new request. And, in fact, in HTTP, there's no guarantee another request would ever come in anyway, so there would be no point in doing so. To get a bean to stick around for another request, you need Session or View scope.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!