This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in ServletContextListener" Watch "Doubt in ServletContextListener" New topic
Author

Doubt in ServletContextListener

Suchen Chodankar
Ranch Hand

Joined: Sep 18, 2006
Posts: 47
Hi all,

I am developing a web app where I am using mysql as a database.
Now I want to build the db connection String using the parameter defined in the <context-param> tag of <web-app>
Here is my context param which I have put under <web-app> tag.


<context-param>

<param-name>serverName</param-name>
<param-value>localhost</param-value>


<param-name>dbName</param-name>
<param-value>testDataBase</param-value>

<param-name>userName</param-name>
<param-value>user1</param-value>

<param-name>password</param-name>
<param-value>password1234 </param-value>
</context-param>

Now when I try to access these parameters for eg: serverName using following code.

ServletContext sc = event.getServletContext();
String serverName = "";
serverName = sc.getInitParameter("serverName");


I am getting the value of serverName as null.


can anyone help me in this regard?

Thanks in advance
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
Did you properly form your xml?

It should be something like this:



Although, I am thinking, you shouldnt be able to deploy it if the xml was wrong.
[ January 13, 2007: Message edited by: Jesus Angeles ]
neetika sharma
Ranch Hand

Joined: May 19, 2006
Posts: 89
Just check your DD , whether it is having the proper entry for
<listener-class> tag, the class which is implementing ServletContextListener.
Suchen Chodankar
Ranch Hand

Joined: Sep 18, 2006
Posts: 47
Thanks guys...

It's working now ...

To
Neetika Sood

<listener-class> tag and the class which is implementing ServletContextListene was proper but the problem was in the <context-param> block.
Then I reformatted my Context param to something like this.

<context-param>
<param-name>serverName</param-name>
<param-value>localhost</param-value>
</context-param>


<context-param>
<param-name>dbName</param-name>
<param-value>testDataBase</param-value>
</context-param>

and it worked.


Although, I am thinking, you shouldnt be able to deploy it if the xml was wrong.



To,
Jesus Angeles

With the previous style which is there in my question that is putting multiple <param-name>,<param-value> pairs in single <context-param> I was able to deploy my application without any error mesaage.


So from this I can say that if you want more than one context-params you cannot put all of them in a single <context-param> block.


Thanks once again.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
Thanks for sharing. Now, I am wondering why it was successfully deployed.
 
 
subject: Doubt in ServletContextListener