Hi John,

One possible way to do that is to create an interface, or an abstract
class, that has an abstract method called, for example, performAction().
You then create objects that implement this interface and store them in
your hashtable using ident as key. To execute the appropriate method,
just get the id from the request, get the appropriate object from the
hashtable using that id, cast it to the interface and then execute the
method performAction(). This works straight forward if you don't need to
pass parameters to the performAction() method, otherwise things have to
get a little more sophisticated, but that's another story ;). BTW, this
is basically the foundation of the command pattern.
Simple example in code:

// The interface
public interface Action
{
 ...
 public void performAction() throws ActionException;
 ...
}

// Some actions
public class ActionA implements Action
{
 ...
 public void performAction() throws ActionException
 {
  //Say AAAAAAAAAAAAAHHHHH
  ...
 }
 ...
}
public class ActionB implements Action
{
 ...
 public void performAction() throws ActionException
 {
  //Say BBBBBBBBBBBBB
  ...
 }
 ...
}

// Prepare the Hashtable
...
  Hashtable actionsHashtable = new Hashtable();
  actionsHashtable.put("A",new ActionA());
  actionsHashtable.put("B",new ActionB());
...

// Use it afterwards
...
  String actionID = (String) request.getParameter("actionID");
  if(actionID != null)
  {
   Action theAction = (Action)actionsHashtable.get(actionID);
   if(theAction != null)
   {
    theAction.performAction();
   }
   ...
  }
...

Things can get, and usually do, a little more sophisticated but that's
the basic idea. I'm actually using this approach, the complicated one
;), and the command pattern so I can verify that it works.
I hope this helps,
Dan
-------------------------------------------
Daniel Lopez Janariz ([EMAIL PROTECTED])
Web Services
Computer Center
Balearic Islands University
-------------------------------------------

> John McDonald wrote:
>
> I searched the archives on this with no luck.
>
> I have a class for which it's entire existance is simply to decide
> what and where the user wants to go. Which means I pass a parameter,
> named "ident", with every request. This class has a boatload of else
> if( ) statements attempting to determine where the user wants to go,
> then I forward them to the appropriate JSP page after I've done some
> work elsewhere in the application.
>
> My question is: Is there a way to store method calls, or the entire
> method, in a hastable and use the "ident" as the key? I could simple
> return an object that would call the appropriate method.
>
> Anyone?? Thanks.
>
> John D. McDonald
> CipherStream Systems
> Phone: 925.373.8700
> Fax:     413.793.6603
> email: [EMAIL PROTECTED]
> web: www.cipherstream.com
> -------------------------------------------------------
> Secure E-Business Is Our Business
> -------------------------------------------------------

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to