tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Danielle Mortimer <danielle.morti...@entrust.com>
Subject RE: ClassCastException trying to access UserDatabase from example s web app
Date Thu, 27 Nov 2003 18:36:36 GMT
I got around my problem using reflection:

  Context initCtx = new InitialContext(); 
  Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
  java.lang.Object object = envCtx.lookup("userDatabase");

  // This throws ClassCastException		  	
  System.out.println("Database id " + ((UserDatabase)object).getId());

  // This works
  java.lang.reflect.Method m = object.getClass().getMethod("getId", null);
  System.out.println("Database id " + m.invoke(object, null));

But is this the best way?  The comment in server.xml saying "The database
object could be accessed like this:" suggests at least that the developer
who wrote the comment expected the cast to work.

-----Original Message-----
From: Danielle Mortimer 
Sent: Tuesday, November 25, 2003 11:04 AM
To: 'tomcat-user@jakarta.apache.org'
Subject: ClassCastException trying to access UserDatabase from examples web
app


I am getting a ClassCastException when I try to access the global
UserDatabase from a web app.  I believe it is something to do with different
class loaders but I'm not sure how to solve it.  I noticed an old post on
this same topic
http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg88967.html but
there was no response.

Details:

1. Out of the box Tomcat 4.1.29 install.  Using Sun JDK 1.4.1_03.  Windows
2000.

2. Modified server.xml to remove the comments around the ResourceLink in the
examples context:

          <!-- If you wanted the examples app to be able to edit the
               user database, you would uncomment the following entry.
               Of course, you would want to enable security on the
               application as well, so this is not done by default!
               The database object could be accessed like this:

               Context initCtx = new InitialContext();
               Context envCtx = (Context) initCtx.lookup("java:comp/env");
               UserDatabase database =
                    (UserDatabase) envCtx.lookup("userDatabase");
          -->

          <ResourceLink name="userDatabase" global="UserDatabase"
                        type="org.apache.catalina.UserDatabase"/>

3. Created jakarta-tomcat-4.1.29\webapps\examples\test.jsp using the sample
code from server.xml:

          <%@ page import="javax.naming.*" %>
          <%@ page import="org.apache.catalina.UserDatabase" %>
          <html>
          <body>
          <%
              Context initCtx = new InitialContext(); 
              Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
              java.lang.Object object = envCtx.lookup("userDatabase");
              out.write( "Database id " + ((UserDatabase)object).getId());
          %>
          </body>
          </html>

4. Copied UserDatabase.class from
jakarta-tomcat-4.1.29\server\lib\catalina.jar into
jakarta-tomcat-4.1.29\webapps\examples\WEB-INF\classes\org\apache\catalina\U
serDatabase.class so the jsp will compile.

5. Restart Tomcat.

6. Go to http://localhost:8080/examples/test.jsp, browser displays
exception:

org.apache.jasper.JasperException
 at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:2
54)
...

root cause

java.lang.ClassCastException
 at org.apache.jsp.test_jsp._jspService(test_jsp.java:51)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
...

Thanks in advance for any suggestions.

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message