This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlets and inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and inheritance" Watch "servlets and inheritance" New topic
Author

servlets and inheritance

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
I have a problem with my database web application... The parent servlet has a database Connection object and a db statment object which are member variables. They are instantiated in the servlets init() method.
What I wanted was for all the child servlets to use these connection objects in synchronized methods. But this does not seem to be happening as the 'children' are creating their own connection and statement objects. Is there a way to prevent the child servlets from doing this?
Many thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly, you don't want to do this because the Connection object would not be thread safe. If the single Connection is shared by all servlets that extend the base servlet, their queries will overlap and get... confused.
Otherwise you would be able to share them by making them static.
I find that extending servlets doesn't really work. There are reasons to do it and ways to do it, but in practice it would be better to look at 'action' style classes like the patterns that turn up in the Struts framework.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlets and inheritance
 
Similar Threads
how do i force a proxy to appear?
When to use session beans...
Connection Pooling in Servlets
Servlets with JDBC
Initialising Database connections with JSP