This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletContextListener Doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServletContextListener Doubt??" Watch "ServletContextListener Doubt??" New topic
Author

ServletContextListener Doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Hi Guys,

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!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Guys, any thoughts for my post?
VijayKumar Sivagnanam
Greenhorn

Joined: Dec 25, 2006
Posts: 26
Hi Jothi,

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.

Hope this will clear your doubt.


SCJP 1.2 <br />SCWCD (94%)<br />SCBCD (preparing)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Vijayakumar,

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??
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.


~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Sanjeev,

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.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Thanks Jothi for clarifying,
Well anything which the container calls is callback?
VijayKumar Sivagnanam
Greenhorn

Joined: Dec 25, 2006
Posts: 26
right sanjeev.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Thanks for the information Sanjeev regarding to container callbacks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContextListener Doubt??