"K.V. Chandrinos" wrote:
> I am in doubt if both these techniques are GUARANTEED to work because they
> involve servlet casting. Unless the point raised for example by Jason
> Hunter in his book is false, this casting might fail for quite legitimate
> reasons. Am I right? Or does this set/getAttribute() methodology mimicks
> an interface implementation, hence by-passing the problem?
>
The argument to a setAttribute function is an Object, as is the return value from
getAttribute. This means that you have to use a cast to utilize your own classes
(which are all subclasses of Object). It is the programmer's responsibility to
ensure that you only do compatible casts, and to catch a ClassCastException if
one occurs. It's exactly the same issue that happens when you use Hashtable or
Vector -- you must ensure that you know what class you are expecting when you
take something out of these collections.
>
> While you are at it, pls. be kind enough to comment on the following
> (rather academic) question:
>
> Since the specs allow a servlet runner implementation in which several
> contexts are created (e.g. for load balancing) how can one get all the
> servlets currently loaded? Quite naturally, any servlet can only query on
> its own servlet context. Is there a server.getServletContext*s*() missing
> or am I missing something?
>
Load balancing isn't really the issue you need to worry about. From the servlet
spec 2.1 on, there is no way for a servlet to access any other servlet instance,
even within its own servlet context. In other words, the
ServletContext.getServlet() method in the 2.0 spec has been deprecated, and will
always return null.
You cannot access other servlet contexts (perhaps running on other servers)
directly either -- this is fundamental to the way Java works. You can only
reference things that are running in the same Java Virtual Machine (JVM) as you
are. If you need to access information in other (load balanced) versions of the
same context (say, to synchronize changes to a global variable), you're going to
need to use some technique such as RMI or CORBA, or native socket connections,
for this purpose.
>
> Thank you,
> Kostas
>
Craig McClanahan
>
> P.S. Would you know of a servlet runner that has elected to implement this
> 'feature' of various contexts?
>
Apache JServ <http://java.sun.com> has a variant of this approach to load
balancing. The idea is that you configure several contexts (perhaps running on
separate machines) with identical servlets. When a request that is not part of a
session is received, it is randomly assigned to one of the servers implementing
the appropriate context. Once you start a session, it stays on that particular
context throughout its life.
This functionality is in the process of being added to Jakarta's Tomcat
<http://jakarta.apache.org> as well -- hopefully to be completed and tested in
time for the 3.1 final release.
___________________________________________________________________________
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