tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edward Demetrio Pacheco <...@melior.com>
Subject ClassCastException accessing loaded servlet from JSP
Date Thu, 11 May 2000 23:52:38 GMT
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.



Mime
View raw message