Hi,

I'll put my 2c while most of you are sleeping :),

Kevin Duffey wrote:
>
> Hi,
>
> >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?
>
> Yeah..how do we do this? :) I would also guess in the init() call to do
> something like:
>
> {
>   // read in XML config file for action class names.
>
>   // load each instace with something like:
>   for( int cntr = 0; cntr < xml_read_in_class_names; cntr++ )
>   {
>     Action action = (Action)
> Class.forName( xml_read_in_class_names[cntr] ).newInstance();
>   }
> }

To make sure you have just one instance, you might want to use two
hashtables as Craig suggested. In my case, what I do is having just one
hastable(JDK1.1 compatibility, Kevin ;)) that maps action names to
action factories. These action factories are objects that create
instances of the appropriate action class, they are initialized at
init() time and, right now, they create a new instance per request. I
might change the implementation later to use just one instance, like
Craig does, or convert them into action pools without changing the
controller code so I'm safe.
My designs tend to be paranoid about adapting to changes and flexibility
which sometimes is an overkill but others can save your day.

> >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)
>
> Use HashSet? HashTable is thread safe I believe, but takes a performance hit
> because of it. Since the only time you "write" to the HashSet is in the
> init() method, and the rest of the time, when all requests come in, its just
> searching and reading the HashSet, I believe you are "safe" from having to
> use any thread-safe object like HashTable. Anyone else?

As Craig, JDK1.1 compatibility is the issue as I had to change my last
application 4 times to different servers with different OS's and for
some of them, there's still no JDK1.2 available (or at least for the OS
version that we have).

> >Does this make sense or am I missing something still?
>
> Seems about right to me. That is pretty much how I was going to implement
> it, except for HashSet for performance reasons. My main concern is the > time
> it takes to look up the object. If you have 1000 hits at one time, can you
> handle 1000 "lookups" at the same time..or is it too slow to do this?

I don't think there's a faster way to look up a set of objects from a
key than a hash (table or set) and anyway, if you have 1000 simultaneous
accesses I would bet you'd start having greater performance problems in
other places like your database handling 1000 connections, your
container handling 1000 threads... but we don't have such a traffic so
I'm not an expert on this kind of issues.

> >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 have had a similar question. I am curious..I know its semantically correct
> to throw exceptions, but this seems odd to me if your trying to squeeze
> every last bit of performance you can out of your code. Seems to me it is
> much faster to return an int value, that could be used specifically as a
> lookup index in the hashset for the forwarding URL, than to throw
> exceptions. Again..I know the "OOP" way is to throw the exceptions, but > the
> overhead in creating the object, even if minimal, I can't imagine in the
> case of 1000 simultaneous hits would be almost as fast as just returning > an
> int value as the result of the action. I have taken a step in creating a
> static class that defines names for the return values, thus I have > something
> like
>
> public static final   ENROLLMENT_GOOD       = 100;
> public static final   ENROLLMENT_BAD        = 101;
>
> public static final   LOGIN_GOOD            = 200;
> public static final   LOGIN_BAD             = 201;
>
> etc..
>
> then, back in the controller servlet, it looks something like:
>
> int result = action.perform(this,request,response);
>
> String forwardUrl = (String)forwardTable.get(result);

I use exceptions as it allows me to put the code that handles non usual
cases (exceptions ;)) away from the code that handles correct ones so
this latter code looks cleaner. I also use them because if I have to
handle an exceptional case, I take it as it is not necessary to hurry
that much as the user is going to get an error message so... Last, but
not least, I use exception as it allows me to specify more complex
errors, I extend the class exception and introduce cause, possible
solution, ... and when debugging, a exception carries more meaninful
information (like the stacktrace) than a plain integer. But again, it's
a matter of taste and in some extreme cases where performance is so
critical you might skip this part(and then regret maybe later down the
road ;) ).


> >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 :)
>
> Actually..I don't mind. ;)

In my case, I was kind of reluctant to do so because I'm also kind of
paranoid about documentation and it's not finished yet (haven't had time
to) but if anyone wants to have a look at my implementation of the model
2 architecture, go to http://www.uib.es/c-calculo/scidlj/leaf/
It contains source code, it's free software so go and have a go if you
want.
Regards,
Dan
-------------------------------------------
Daniel Lopez Janariz ([EMAIL PROTECTED])
Web Services
Computer Center
Balearic Islands University
-------------------------------------------

===========================================================================
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

Reply via email to