File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes ServletContextListener Class fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContextListener Class fails" Watch "ServletContextListener Class fails" New topic
Author

ServletContextListener Class fails

Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Hi all!
I have created a class that implements the ServletContextListener class

then I have added the following to the web.xml.

then I've added som <context-param>. The listener compiles and runs and I can see when the tomcat starts that it is ok. Now when I try to access a servlet I can do this ones and after the tomcat exists. there must be something wrong with the listener class. I can only access this method ones and then the Tomcat server shuts down.

does anyone know whats wrong? As a paranthese I just wonder where to stored database connection info. Previous i stored it i java code but it wasn't so good because I have to change all files if ex password changed. Now I store it in web.xml but I feel it is security risks with this. What is the proper way to do it?
// Mathias
[ November 03, 2003: Message edited by: Mathias Nilsson ]

SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
Celina Paul
Greenhorn

Joined: Nov 01, 2003
Posts: 16
Hi Methias,
I tried the same code for the ConnectionHandler and tested it with another simpler servlet in the same context. It worked fine for me and did not crash the Tomcat server.
I could get the context attribute and also use the connection.
The only questionable thing that I see in your isLoggedIn() method call is why are you closing the connection ? This connection should get closed when the context is destroyed...
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Also, the reason Tomcat is exiting is because you tell it to in your finally block...

System.exit( 0 );

This should never be coded in any server-side app. It will not only end your program (your web application), but it ends Tomcat as well (and any other web-app being hosted in Tomcat).

You are probably experiencing some sort of trouble with your driver, as you mentioned in
this thread
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
I see it in my code now thanks to you two.
//Mathias
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
There is something I still don't understand. Can I only have one connection
open per application? Can I only have one instance of the Connection object. ( I now Connection is an interface but I couldn't describe it in a better way )

// Mathias
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

There is no reason you cannot have more than one.

However, how you've implemented this particular class, there is only ever one Connection object available under the context attribute "connection".

As it stands, every page (and perhaps more than one at a time) will be using this one connection; which is not really recommended.

What you want to think about is how to implement a 'pool' of connections instead (if you're doing this for practice). If you're building an actual app, then start looking into using JNDI DataSources or pre-built connection pool libraries. Just google for more info.
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Thanks Mike!
I still don't understand. If I use the listener above and then in my isLoggedIn() method replace the

with

the system will crash and terminate tomcat? Is it still the same connection object?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

right.

This is probably why using Access sucks. Access is meant as a single-user database. It doesn't handle more than one connection very well. I'd say "not at all", but I'm not 100% sure of it. I think Access tries to do concurrent connections by doing some sort of row-locking funny business. But this may not be available through the jdbcodbc bridge.

Bottom line.. switch to MySql.
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Ok! I will try MySQL to see how it works.
// Mathias
 
Consider Paul's rocket mass heater.
 
subject: ServletContextListener Class fails
 
Similar Threads
contextDestroyed- odd behaviour?
servletcontextlistener
listener tag is not working in Tomcat
Connection pool
sl314.util.sql.ConnectionPool