<SNIP>
> Dynamic class loading (without fleshing out the error
> handling) looks like this:
>
> String actionClassName = ...; // Whatever action class you need
> Class actionClass = Class.forName(actionClassName);
> Action action = (Action) actionClass.newInstance();
How do I make this work with the HashTable/Map/Set so that I can ensure only
a single instance of any action class? I assume I can load all the action
classes into a HashSomething on the servlets init() and get the appropriate
action and call perform()? Is this correct?
So in effect I would have something like this in init? (forgive the errors
here)
HashTable actions = new HashTable();
// do this for all the action classes (assuming the above code is
used to load them)
actions.add(actionName, actionObject);
//also create the HashTable of requestURI > actionName mappings?
HashTable mappings = new HashTable();
// do this in properties file or init() method. . .
mappings.add(requestURI, actionName);
Then when a request comes in (in doGet() or doPost())
// check the requestURI > actionName mappings
String actionName = (String) mapping.get(request.getRequestURI());
Action action = (Action) actions.get(actionName);
action.perform();
I left out all the normal error checking that would be performed to make
sure the object actually exists etc.
Does this make sense or am I missing something still?
> For semantic errors (does this integer field accept negative
> numbers? is the
> customer account number valid?) I prefer to do those checks
> inside the bean, and
> throw an exception (usually IllegalArgumentException or
> IllegalStateException) if
> the data has the right type, but is not valid according to
> the business rules.
In my case, where I am using a variety of custom error messages depending on
the field that caused the error (if the field doesnt accept negative
numbers, it would be a special error message for the field which would then
show up in the original form) does it make more sense to perform the
semantic error checking in the servlet? It seems that throwing an exception
is not necessarily enough information for me. Or even both (semantic
checking in the servlet, but also throw exceptions in the bean if the bean
object is reused in a non web application)?
> I wish I could show you complete examples, but the apps I've
> done this way are
> proprietary, and I haven't yet had the opportunity to build
> an example (based on
> this architecture) that I can share. Maybe someday soon ...
I would love to see an example of your architecture, but i realize that the
last thing someone wants to do after a long day of work is write examples
for the rest of us :)
> I did my first Model 2 app without this kind of a mapping.
> What happened to me is
> that I later wanted to reassign all the "filename.jsp" names
> to a more natural
> naming convention -- it was the kind of thing that happens
> occasionally as an
> application grows over time. Unfortunately, that meant had
> to go back and do
> surgery on all of my action classes to correct these (of
> course, like a fool, I
> used literal string values instead of constants in some
> shared object, so this was
> pretty painful). If I had had the logical mapping table, it
> would have been one
> set of changes in one place to fix this.
I will take this into consideration, and hopefully my boss does too :)
Thanks again, helped quite a bit :)
Jeff
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets