tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Edward Demetrio Pacheco <...@melior.com>
Subject Re: ClassCastException accessing loaded servlet from JSP
Date Fri, 12 May 2000 16:41:02 GMT
Hi Karen,

Thanks for the helpful feedback!  I did find a workaround for the problem
which I thought I would post to the list also.  It turns out that if I put the
servlet classes in the system CLASSPATH or in the /WEB-INF/classes directory
of the context (as a jar file),  then everything seems to work just fine: no
more ClassCastException...

Edward

Karen Lease wrote:

> 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
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org


Mime
View raw message