From klease@vftis.com Fri May 12 08:27:17 2000 Return-Path: Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 6701 invoked from network); 12 May 2000 08:27:17 -0000 Received: from smtp2.cluster.oleane.net (195.25.12.17) by locus.apache.org with SMTP; 12 May 2000 08:27:17 -0000 Received: from karen (dyn-1-1-174.Def.dialup.oleane.fr [195.25.5.174]) by smtp2.cluster.oleane.net with SMTP id KAA80296 for ; Fri, 12 May 2000 10:26:58 +0200 (CEST) Received: by localhost with Microsoft MAPI; Fri, 12 May 2000 10:26:46 +0100 Message-ID: <01BFBBFC.9370D020.klease@vftis.com> From: Karen Lease Reply-To: "klease@vftis.com" To: "'tomcat-user@jakarta.apache.org'" Subject: RE: ClassCastException accessing loaded servlet from JSP Date: Fri, 12 May 2000 10:26:45 +0100 Organization: SPX Valley Forge X-Mailer: Microsoft Internet E-mail/MAPI - 8.0.0.4211 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N Hi Edward, This could be caused by a ClassLoader problem. If a different class loader is used for the connection pool servlet and the JSP, you will get this exception. For example, I have seen it after servlet reloads, because tomcat uses a new class loader for the webapp when it reloads any servlet in that webapp. I have somewhat similar code which is working, although my attribute is in Session rather than Application scope, and I'm also accessing it from a custom tag class which is called from my JSP. Just an opinion on servlet programming style : you might want to create a separate ConnectionPool object from your servlet rather than using the servlet itself as the object which you store in the context. Hope this gives you some ideas, Karen Lease On Friday, May 12, 2000 12:53 AM, Edward Demetrio Pacheco [SMTP:edp@melior.com] wrote: > I'm hoping someone can help me with an odd problem: I've set up a > db > connection pool servlet that is loaded on startup. In that servlet, I > am > using the setAttribute() method of the ServletContext to allow the > servlet > itself to be accessed as an application object. From another servlet, > I am > able to access this connection pool servlet with no problem. However, > if I > try to do the same from a JSP page, I get a ClassCastException error. > See the > code snippets below for more info... > > Thanks. > > ====================== > Code in connection pool servlet > ====================== > > // Context attribute key > public static String KEY = "javaservlets.db.ConnectionServlet_21"; > . > . > // Add this servlet to the context so that other servlets > // can find it > getServletContext().setAttribute(KEY, this); // this is in the init() > of the > servlet itself > > =================================================== > Code in a different servlet that uses the connection pool servlet > (this works) > > =================================================== > > // Get the ConnectionServlet that holds the > // connection pool > ServletConfig config = getServletConfig(); > ServletContext context = config.getServletContext(); > Object o = context.getAttribute(ConnectionServlet_21.KEY); > if (o == null) { > throw new ServletException("ConnectionServlet not started"); > } > ConnectionServlet_21 conServlet = (ConnectionServlet_21) o; > > ================================= > Code in JSP file that causes ClassCastException > ================================= > > javaservlets.db.ConnectionServlet_21 conServlet = null; > synchronized (application) { > conServlet = (javaservlets.db.ConnectionServlet_21) > > pageContext.getAttribute("javaservlets.db.ConnectionServlet_ > 21",PageContext.APPLICATION_SCOPE); > > // ^ I believe this is the line it has the problem > with. It > can get the application attribute, but can't cast it... > if ( conServlet == null ) { > throw new ServletException("ConnectionServlet not started"); > > } > } > > Also, using the code that works from the servlet in the JSP likewise > fails > with the ClassCastException. > > > > ---------------------------------------------------------------------- > ---- > To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org > For additional commmands, email: tomcat-user-help@jakarta.apache.org