Justin Wells wrote:
> Use dynamic class loading to your advantage. Create an interface
> representing processing of a request--containing the method you
> need to call. Create several implementations of it, one for each
> way of processing.
>
> Then create a "reactor" which loads and dispatches these handlers.
> ie: You supply a key to the reactor (from your form var) and it looks
> up the right handler for you--possibly out of a hashtable, or by
> searching a directory, or, easiest, by calling Class.forName().
>
> Next either you or the reactor call the method on the returned
> implementation.
>
I've used this design pattern to good effect on several projects. Besides being
useful in a pure servlet world, it is also quite nice in a JSP-based "Model 2"
design (from the JSP 0.92 specification), to select the processing to be
performed by the servlet before it forwards to whatever JSP page should be
displayed next.
In addition to using a hidden form variable to pass the selector key, you might
also consider using the "extra path" information (i.e. everything in the URI
after your servlet name). That way, you can use this technique in simple GET
requests as well, without having to remember to add a query parameter -- it feels
like you are addressing a separate servlet for each type of request, but the
servlet engine uses the same servlet to handle them all.
Using parameters: /servlet/myservlet?action=action1
Using path info: /servlet/myservlet/action1
The results are the same either way, so it's a stylistic preference.
>
> The other way WebMacro does it may also be suitable for you:
>
> Declare a superclass servlet which does the generic processing for
> you and then calls an abstract method. Make one subclass of this
> for each different mechanism.
>
> WebMacro manages some static data in the abstract superclass so
> that database connections and other expensive resources can be
> shared between connections and servlets. Thus is a bit tricky,
> because you must carefully reference count the statc data and
> clean it up when the last subclass instance is destroyed. Do this
> by incrementing a count on init() and decrementing on destroy().
>
> WebMacro is free, so you can download it and look at the code for
> more details.
>
> Look at the Reactor.java and WMServlet.java classes, and things
> related to them.
>
> WebMacro is at:
>
> http://www.webmacro.org/Download.html
>
> Justin
>
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html