You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jill Stephenson <ji...@jabcreative.com> on 2001/02/12 02:52:03 UTC

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

Re: Using a servlet superclass

Posted by Jim Rudnicki <jd...@pacbell.net>.
> 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.,
...snip...
> 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 ?

Answer 1: No
----

Answer 2: But wait, you can make the init() method in SuperServlet FINAL.
Then when someone tries to overide init(), the compiler will say no.

Now add empty methods
initExtend() and initExtend(ServletConfig c) to the base class.

Call these methods _last_ in SuperServlet's init methods.

Document that derived classes should implement these methods if they require
additional initialization.  With this approach the mistake cannot be made.

nite,
Jim


Re: Using a servlet superclass

Posted by Dale Thatcher <da...@88.net>.
Hi,

A quick comment on this technique.  You must be careful when setting variables
in the childClass if you call the init method from the constructor.  For example
the following code produces unexpected results:

class Child extends Parent {
    int value1 = 0;
    int value2;

    public void childInit() {
	value1 = 5;
	value2 = 10;
    }

    public void printValues() {
	System.out.println("value1: " + value1);
	System.out.println("value2: " + value2);
    }
}

public abstract class Parent {
    public Parent() {
	childInit();
    }

    public abstract void childInit();

    public static void main(String args[]) {
	Child c = new Child();
	c.printValues();
    }
}

The output on a compliant JVM will be:

value1: 0                       
value2: 10                      

This is because the child's constructor is called after childInit.  I say
compliant because some non-sun compilers (cafe and possibly others) produce
different results.

thanks,

- Dale Thatcher

On Sun, Feb 11, 2001 at 09:09:13PM -0500, CPC Livelink Admin wrote:
> 
> Yes.
> 
> Have your WorkerServlets implement a procedure called childInit or
> something, which is basically an empty procedure in the SuperServlet.  Then
> call this function as the last (or first) call of the SuperServlet init
> function. This is much like how GenericServlet makes init() a convenience
> call instead of init(ServletConfig config).
> 
> Regards,
> Paul
> 
> 
> -----Original Message-----
> From: Jill Stephenson [mailto:jill.s@jabcreative.com]
> Sent: Sunday, February 11, 2001 8:52 PM
> To: 'tomcat-user@jakarta.apache.org'
> 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: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org

RE: Using a servlet superclass

Posted by CPC Livelink Admin <cp...@fitzpatrick.cc>.
Yes.

Have your WorkerServlets implement a procedure called childInit or
something, which is basically an empty procedure in the SuperServlet.  Then
call this function as the last (or first) call of the SuperServlet init
function. This is much like how GenericServlet makes init() a convenience
call instead of init(ServletConfig config).

Regards,
Paul


-----Original Message-----
From: Jill Stephenson [mailto:jill.s@jabcreative.com]
Sent: Sunday, February 11, 2001 8:52 PM
To: 'tomcat-user@jakarta.apache.org'
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: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, email: tomcat-user-help@jakarta.apache.org



Re: Using a servlet superclass

Posted by Tom Woteki <dr...@woteki.com>.
Perhaps this will work, though I am not so certain of the life cycle of a
servlet to say for sure:

Define the default (i.e. no argument) constructor for SuperServlet (its
signature is: SuperServlet() ) within which you call init. Any child of
SuperServlet should then automagically call init (or any override of it)
when its own default constructor is invoked: default constructors are
automatically invoked to the top of the inheritance hierarchy.

Dr. Wo
-- 
Tom Woteki, a.k.a. Dr. Wo
mail to:<dr...@woteki.com>
202-544-2743 (fax)
http://www.woteki.com/wines <-- a pure Java, Apache-Tomcat web application
running under MacOSX

> From: Jill Stephenson <ji...@jabcreative.com>
> Reply-To: tomcat-user@jakarta.apache.org
> Date: Sun, 11 Feb 2001 20:52:03 -0500
> To: "'tomcat-user@jakarta.apache.org'" <to...@jakarta.apache.org>
> 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: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, email: tomcat-user-help@jakarta.apache.org
>