Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

having trouble Initializing database connection from within ServletContextListener  RSS feed

 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to initialize an Oracle database connection as my servlet gets loaded.
Following is the code in my class ,MyListener.java, which implements ServletContextListener.




And here is the class,MyDatabase.java, which creates and uses the connection:



Here is my servlet class, MyServlet.java:

but when I am sending request for the servlet, I am getting the following error message in my browser:



And in My Server logs I can see the message :
class Not found exception
which is in the MyDatabase class's catch block.How to fix this??
 
Rajesh Ronald Reagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rao,

For the entire web application, there will be only one ServletContext.

ServletContext is created when a new deployment is made after that this ServletContext will be used by all the jsp's and servlet's in the web application.


The NPE is thrown because , the ServletContextListener is not initialized.

You should instruct the container.

Kindly check whether you had your entry in the deployment descriptor ?

If not please add the below code.




Problem is the listener is not initialized.

If you have any queries you can checkout the following link.

http://www.javatips.net/blog/2011/12/servletcontextlistener-example

Dont forget to report back

Regards,
Ronald
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh Ronald Reagan wrote:
The NPE is thrown because , the ServletContextListener is not initialized.

Kindly check whether you had your entry in the deployment descriptor ?
Ronald

Hi Rajesh,
You are right here. But this was not the problem. I got my problem solved by coping ojdbc14.jar file in my
tomcat_home/webapps/context_root/WEB-INF/lib folder.
I don't understand, why is that required?
I already have set path to ojdbc14.jar in my user environment variables.
And that is working for me in case of Non-servlet programs.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!

Similar Threads