Re: Wrox and Servlet

2000-10-29 Thread Hans Bergsten

"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:

  

or the pure Java web server my company makes, 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



Wrox and Servlet

2000-10-27 Thread Parker, Karl

Good morning all.  I am a stodgy old VB, COM, ASP, C++ programmer and am
trying to convert to a hip, young Java type.  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.   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.  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
at
com.wrox.context.chat.ListRoomsServlet.writePage(ListRoomsServlet.java,
Compiled Code)
at
com.wrox.context.chat.ListRoomsServlet.doGet(ListRoomsServlet.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:701)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:826)
at
com.netscape.server.http.servlet.NSServletRunner.Service(NSServletRunner.jav
a:502)


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

BTW,  I assumed this wasn't covered in a SUN FAQ, so I hope this isn't too
junior a question.


Regards,

Karl D. Parker
Core Technologies
RetailDNA, LLC
<> - A
Walker Digital Company

One Stamford Plaza
Stamford, CT 06902
203-564-1458 | 203-564-1772 (fax) | 330-8051272 (cell)
This message, together with any attachments, is intended only for the use of
the individual or entity to which it is addressed and may contain
information that is legally privileged, confidential, and exempt from
disclosure. If you are not the intended recipient, you are hereby notified
that you have received this message in error; any review, dissemination,
distribution, or copying of this message, or any attachment, is strictly
prohibited. If you have received this message in error, please delete this
message and any attachments from your computer and immediately notify the
original sender at (203) 564-1432 or by return e-mail. Thank you

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