• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in ServletContextListener

 
Suchen Chodankar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing. Now, I am wondering why it was successfully deployed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic