jayashreek wrote:
>
> I have encountered a problem in the code given on page 84-85 of chapter
> 5 in the book Professional Java Server Programming.
>
> The code for the listroomservlet is as follows :
>
> public void doGet(HttpServletRequest req, HttpServletResponse res)
> throws IOException
> {
>
> res.setContentType("text/html");
> PrintWriter out = res.getWriter();
>
> String expand = req.getParameter("expand");
> HttpSession session = req.getSession();
> String avatarName = (String) session.getValue("avatarName");
> if (avatarName == null)
> {
> avatarName = "";
> }
>
> writePage(out, expand, avatarName);
> out.close();
> }
>
> private void writePage(PrintWriter out, String expand, String
> avatarName)
> {
>
> out.println("<HTML>");
> out.println("<HEAD><TITLE>Chat rooms</TITLE></HEAD>");
> out.println("<BODY>");
> out.println("<H1>Chat rooms</H1>");
> out.println("<FORM METHOD=POST ACTION=chatroom>");
>
> // Add radio boxes for selecting a room
> out.println("Select the room you like to enter " +
> "or click on a name to see the description:<P>");
> roomlist rl = (roomlist) getServletContext().getAttribute("rl");
> Enumeration rooms = rl.getrooms();
> boolean isFirst = true;
> while (rooms.hasMoreElements())
> {
> chatroom room = (chatroom) rooms.nextElement();
> String roomName = room.getname();
> out.println("<INPUT TYPE=RADIO NAME=roomName VALUE='" +
> roomName + "'" +
> (isFirst ? " CHECKED" : "") + ">" +
> "<A HREF=listrooms?expand=" +
> URLEncoder.encode(roomName) + ">" +
> roomName + "</A><BR>");
> isFirst = false;
>
> // Show description if requested
> if (expand != null && expand.equals(roomName))
> {
> out.println("<BLOCKQUOTE>");
> out.println(room.getdescription());
> out.println("</BLOCKQUOTE><BR>");
> }
> }
>
> // Add a field for the avatar name
> out.println("<P>Enter your name: ");
> out.println("<INPUT NAME=avatarName VALUE='" +
> avatarName + "' SIZE=30>");
>
> // Add submit button
> out.println("<P><INPUT TYPE=SUBMIT VALUE='Enter'>");
> out.println("</FORM>");
> out.println("</BODY></HTML>");
> }
>
> and the error that I encounter is as follows :
>
> Chat rooms
>
> Select the room you like to enter or click on a name to see the
> description:
>
> Error. The server encountered an unexpected condition which prevented it
> from fulfilling the request.
>
> java.lang.ClassCastException
> at listroomsservlet.writePage(listroomsservlet.java, Compiled
> Code)
Most likely, this is due to the way class loading works in ServletExec.
I assume you have all chat application classes stored in the special
servlets directory. ServletExec loads classes found there with a
separate class loader per servlet. This means that the each servlet gets
its own version of shared classes, such as RoomList in this case.
And a class loaded by two different class loaders is *not* seen as the
same class by the JVM. It identifies a class by the combination of
the class name and the class loader that loaded it. Therefore you get
a ClassCastException when you try to cast an instance of the class loaded
by one servlet to the class loaded by another servlet. Servlet 2.2 contains
more strict rules about this, and does not allow a servlet container to
use a separate class loader for each servlet, but in 2.1 (implemented by
ServletExec 2.x) it was allowed.
To solve this, put all classes for the chat application in the system
class path instead (i.e. move them to another directory than servlets
and include the new directory in ServletExec's class path). The classes
will then be loaded by the JVM's primordial class loader and there will
only be one version of each class.
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
___________________________________________________________________________
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