W3C home > Mailing lists > Public > www-jigsaw@w3.org > May to June 1998

ClassCastException while usng getServlet(String)

From: S.Ramaswamy <srswamy@giasdl01.vsnl.net.in>
Date: Sat, 30 May 1998 10:17:08 +0530
To: <www-jigsaw@w3.org>
Message-Id: <01bd8b86$011258a0$743a36ca@matrix>
I am using the following configuration 
a) Jigsaw Web Server from w3c Consortium that supports Servlets
b) JDK1.2 beta 3  
I have two servlets
1)   MxSessionManager    servlet
2)  MxBrowse servlet 
User invokes MxSessionManager first. This gets inited allright. Then user invokes MxBrowse servlet. Within MxBrowse init method, I want the reference to MxSessionManager instance. MxSessionManager maintains session details which is required each and every subsequent servlet call. I am getting the referenc as given below
ServletContext sc = conf.getServletContext();
MxSessionManager sm = (MxSessionManager)sc.getServlet("MxSesssionManager");
This gives me ClassCastException. I have checked the Class object of the returned object of getServlet("MxSessionManager").
This is shown the same as MxSessionManager.  
Option 1 tried
However, if I cast the returned value to HttpServlet, or GenericServlet the superclasses of MxSessionManager, then there is no 
HttpServlet hs = (HttpServlet)getServlet("MxSessionManager");    // works fine

Option 2 tried
I have tried subclassing MxSessionManager say as MxLogin extends MxSessionManager. Now I do 
MxSessionManager sm = (MxSessionManager)sc.getServlet("MxLogin");
still no use.

                            ------------------------------------------------Code Snippet -------------------------------------------

public abstract class HttpServlet extends GenericServlet implements Servlet {
public class MxSessionManager extends HttpServlet {
public class MxSessionHttp extends HttpServlet {
      public void init( ServletConfig conf ) {
try {
     super.init( conf );
} catch( ServletException e ) {
 try {
      MxSessionManager sessionManager = (MxSessionManager)
} catch( Exception e ) {
************ I am getting ClassCastException here.  ***********

Can someone help please? 
Received on Saturday, 30 May 1998 00:47:25 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:25:32 UTC