This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
1.) It will not be Garbage Collected (GCed) as long as there is reference to it within the JVM. Binding it to application scope will maintain this reference for the life of your app (unless you remove it).
2.) I would create this bean in a context listener so it will be ready before any servlets are initialized.
3.) A singleton would work. Your check for null would also work.
3) You could do both. Either way, if you instanciate the object in a context listener and bind it to application (context) scope, you won't need to instanciate it from a JSP. It will already be there before your JSPs are ever run. This will be especially helpful if you are using EL.
Joined: Oct 15, 2004
Ben, Thanks, that book seems better than others I have read on servlets/jsps. But I didn't see anything on 'context listeners' - if this is not the same as the ServletContext then this is new to me. Searches on google have resulted in articles relating to EJB's or products that use it, rather than using it in the raw. Could you supply some more guidance please ?
My other option is to use the ServletContext as first mentioned :
but to do it in a servlet that runs on server startup.