• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

ServletContextListener Class fails

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see it in my code now thanks to you two.
//Mathias
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok! I will try MySQL to see how it works.
// Mathias
 
There's a city wid manhunt for this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic