How about this?

1)        Declare your SuperServlet as abstract
2)        Declare an abstract method called SuperServlet.initialize()
3)        Call this.initialize() from SuperServlet.init()

That way all of your worker servlets will be forced to implement initialize().  You then have to trust your programmers to do the sensible thing and put their initialisation code there rather than in init().  You are guaranteed that both methods are called in turn when your servlet initialises.

-----------------------------------------------------------------------------------------------
Aaron Knauf
Implementation Consultant
Genie Systems Ltd
Auckland, New Zealand
Ph. +64-9-573 3310 x812, email: [EMAIL PROTECTED]
http://www.geniesystems.com
------------------------------------------------------------------------------------------------



"Samson, Lyndon [IT]" <[EMAIL PROTECTED]>

13/02/2001 05:26
Please respond to tomcat-user

       
        To:        "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
        cc:        
        Subject:        RE: Using a servlet superclass




Well no simple solutions will do exactly what you want. There is nothing
special about the init method in java. It might be nice if the servlet
container promised to walk up the class hierachy calling each init as it
goes but thats not how it works.

But you could;

1.Use the java debugging interface to detect when the method init is being
called in you derived class and call the base class method.

2.Create your own Method object ( bytecode ) and replace the derived classes
init method bytecode ( is this even possible? ) giving

  newinit = function() {
   call super.init
   call oldinit
 }

  oldinit = derived.init
 derived.init = newinit



Okay, these are a bit tounge in cheek :-) Depends how much you don't want to
call super.init() I guess...



-----Original Message-----
From: Jill Stephenson [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 12, 2001 1:52 AM
To: '[EMAIL PROTECTED]'
Subject: Using a servlet superclass


I have a number of servlets in my application that need to
perform common initialisation, etc.  So I was thinking of
creating a superclass that has an init method that does all
of the common work, eg.,

public class SuperServlet extends HttpServlet {
 public void init() {
   // do common initialisation stuff in here
 } // init
} // SuperServlet

Then all the workers servlets would extend SuperServlet
rather than HttpServlet, eg.,

public class WorkerServlet extends SuperServlet {
 ...
} // WorkerServlet

This seems to be OK, until I implement init in the
WorkerServlet, as the init method in the SuperServlet
does not then get invoked.  While I can call super.init()
in the WorkServlet, I want this to be handled automatically
as I can bet on someone forgetting this step, eg.,

public class WorkerServlet extends SuperServlet {
 public void init() {
   // don't want to have to make this call ==> super.init();
 } // init
 ...
} // WorkerServlet

Is there any way to implement this ?

----
Jill

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]



Reply via email to