You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jim Rudnicki <jd...@pacbell.net> on 2000/05/01 09:20:16 UTC

Re: Q:How to auto-execute a function everytime a servlet is invoked.

Rishi,

I believe I understand what you want

class TrackedServlet extends HttpServlet {
   public void doGet( HttpServletRequest request, HttpServletResponse
response ) {
    do common action
    call doGet() in DerivedServlet
    do common action.
   }
}

class DerivedServlet extends TrackedServlet {
   public void doGet( HttpServletRequest request, HttpServletResponse
response ) {
      create page
   }
}

You want Tomcat to call TrackedServlet.doGet, which in turn would execute
the DerivedServlet doGet().  The effect is wrapping the derived class
behavior with that in the base class.  The language won't support this.
Tomcat will always see the derived class method.

C++ templates would allow a solution, but I have another good solution.

My advice: think simple.

abstract class TrackedServlet extends HttpServlet {

   protected abstract void innerDoGet( HttpServletRequest request,
HttpServletResponse response ) ;
   protected abstract void innerDoPost( HttpServletRequest request,
HttpServletResponse response ) ;

   public void doGet( HttpServletRequest request, HttpServletResponse
response ) {
    /* do common action */
    innerDoGet(request, response);
    /* do common action. */
   }
}

class DerivedServlet extends TrackedServlet {
   public void innerDoGet( HttpServletRequest request, HttpServletResponse
response ) {
      /* create page */
   }
   public void innerDoPost( HttpServletRequest request, HttpServletResponse
response ) {
      /* create page */
   }
}

I believe this is the only correct way to shim code before and after the
calls to doGet() or doPost().  A developer only has to know to derive the
servlet from TrackedServlet and implement the abstract methods.  I agree,
the jsp soln is _____(deleted).

gnite,
Jim



----- Original Message -----
From: "Rishi N" <re...@hotmail.com>
To: <to...@jakarta.apache.org>
Sent: Friday, April 28, 2000 4:58 PM
Subject: Re: Q:How to auto-execute a function everytime a servlet is invoked
.
>
> >
> >Hi,
> >For JSP you could use the "extends" directive but it would be
> >far easier to use a bean or just include another JSP page
> >at runtime that does the update.
> >
> ><jsp:include page="update.jsp" flush="true"/>
> >which will include it at runtime?
> >
> >Greg
> >
> >Rishi N wrote:
> > >
> > > hi,
> > >
> > > i tried overwriting just the service method. so my service method runs
> >my
> > > function, and then i just call super.service(req,res), to invoke the
> > > HttpServlet version of service.this seems to work fine. i think i
don't
> >need
> > > to overwrite any of the other methods. is this correct? please advise.
> > >
> > > i also have another problem now. the above trick will work for
servlets,
> >but
> > > how can i handle jsp's? i have no control over how the corresponding
> > > servlets are generated, so i cannot make them extend my class. of
> >course, i
> > > could probably modify the tomcat code to make sure they use my
servlet,
> >but
> > > that seems to be a very dirty hack. is there any other way of doing
> >this?
> > >
> > > thanks,
> > > risi