This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Actually I was trying to create a sample login screen where I use oracle as backend and I have a simple table that stores the list of usernames and password. I want to use Listeners to connect to my database. So how do I do it?? What do I actually put in my contextInitialized?? I guess that I can connect to the database from the contextInitialized and use contextDestroyed for closing the connection. Am I right?? Then what do I set for <param-name> and <param-value> in the DD??
Also, I have this doubt is that in the example given in the HFSJ book on page 171 states that we already set the dog Breed to GreatDane?? Then what is the real use of using Attributes?? I can well go with a parameter rather than attributes?? Am I confusing myself?? Guys please help!
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Using the <param-name> <param-value> in the DD, you can set the DB URL, user name and password which you can retrieve in the ServletContext using getInitParameter(). You can get only String and NOT Objects using parameter. Also you cannot set the parameters programmatically. You can set an object as an attribute.
For your scenario, you can create a connection object in the contextInitialized() method, getting the context parameters and set the connection object as attribute in the ServletContext. Since ServletContext is available for the whole application, you can get the connection object using the getAttribute() method.
I think, I'm getting this. As you said, I can use any name for the <paran-name> and <param-value> elements, just any name, it can be anything and convert that name which is a String to actual Object using the Listener class and set it as an attribute in the scope that I want.
So when the container starts up, it reads my DD and before I could querry my database, the connection is ready for me. Wonderful concept!
So, is there any other place where I can have a similiar functionality (other than doing it declaratively) wherein I can have the container do all the things beforehand??
Jothi, I am a bit confused with your post.Generally contextInitailized() and contextDestroyed() which are the call back methods are being used by the container when a new context is initiazed (some startup) and destroyed(some cleanup). Why are you trying to get the connection object in contextInitialized() method?What's your assumption when a new context should be initialized? Your context should not be initialized unless you are validated,and to be validated you need a database connection.
The point is that, contextInitialized and contextDestroyed are not container callback methods, they are the methods in the ServletContextListener interface and they are the ones to run first when the container starts up. So you can do your database connection using this. So when you request for a servlet that has some operations to be carried on with the database, you already have the connection opened.
Joined: Nov 01, 2006
Thanks Jothi for clarifying, Well anything which the container calls is callback?