This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and inheritance" Watch "servlets and inheritance" New topic

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

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:
subject: servlets and inheritance
It's not a secret anymore!