"Parker, Karl" wrote:
>
> Good morning all. I am a stodgy old VB, COM, ASP, C++ programmer and am
> trying to convert to a hip, young Java type.
Welcome to the "hip side" ;-)
> I am pushing through the first
> chapters of "Professional Java Server Programming" published by Wrox. I
> tried using Apache Server and the Sun SDK directly but found better luck
> using Netscape's Fastrack server to test some of these Servlet examples.
Which version? Are you using the native servlet support or a separate
add-on container? See below for why this is important info in order to
help you.
> I believe I finally have the server configured right and am trying to use
> persistent storage in a servlet context that is launched on server startup.
> The application is a sample chat room program.
I'm actually the guy who's guilty of writing this code for the first edition
of the book. I haven't studied what's in the second edition (J2EE) of the
book yet though, so things may have changed. Which edition of the book do
you read?
> However when this code is executed later:
>
> RoomList roomList = (RoomList) getServletContext().getAttribute("roomList");
>
> The web page gives this:
>
> Server Error
> This server has encountered an internal error which prevents it from
> fulfilling your request. The most likely cause is a misconfiguration. Please
> ask the administrator to look for messages in the server's error log.
>
> And the server's error log shows this:
>
> [27/Oct/2000:07:26:17] failure ( 1524): Internal error: exception thrown
> from the servlet service function (uri=/ListRooms):
> java.lang.ClassCastException, Stack: java.lang.ClassCastException
> [...]
Most likely this is caused by an "incorrect" use of class loaders in the
servlet container. I believe the native servlet container in Netscape's
3.x servers used one class loader per servlet, and some of the add-ons
did the same. This may still be true for 4.x (iPlanet) servers. See
more below.
> I put this code before it to determine the class because I assumed (as a C++
> guy) that there is some sort of casting error:
>
> Object ob = getServletContext().getAttribute("roomList");
> if(ob==null)
> {
> out.println("Not set!");
> }
> else
> {
> out.println(ob.getClass().toString());
> }
>
> which results in:
>
> class com.wrox.context.chat.RoomList
>
> which is the type defined. Anyone have any ideas to keep me from chucking
> my Java books out the 9th story window potentially hurting potential
> customers ???
The thing is that a class is identified by two things in Java: it's name and
the class loader that loaded it. In this case it looks like the class that
the object in the servlet context is an instance of is loaded by a different
class loader than the class loaded by the ListRoomsServlet.
One way to, typically, solve this is to place all classes in the main
classpath for the servlet container, as opposed to in the special "servlets"
directory. Servlet containers that use a separate class loader per servlet
typically only use this class loader for the classes found in the special
directory, and delegate the loading of all other classes to the primordial
class loader. The result is that classes located in the main classpath get
loaded by the primordial class loader even in this scenario. Hence, you avoid
the ClassCastException issue.
It's been so long since I played with Netscape servers, but I believe there's
an option for adding directories and JAR files to the main classpath in the
admin interface somewhere. You should be able to find it if you read the
docs. Same thing if you use an add-on servlet container.
But I actually recommend that you use a product that's easier to configure
while you're learning this stuff, such as Tomcat 3.2 (*not* 3.1) stand-alone:
<http://jakarta.apache.org/>
or the pure Java web server my company makes, LiteWebServer:
<http://www.gefionsoftware.com/LiteWebServer/>
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
===========================================================================
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