Link to home
Start Free TrialLog in
Avatar of msmolyak
msmolyak

asked on

ClassCastException in the servlet

I get the ClassCastException in my servlet class while enumerating java serilized object. The code looks like :

doPost( ...., ,.....) {

 Vector v = ejb.getAllProducts();
 Enumeration e = v.elements();
 while (e.hasMoreElements() ) {
   // Java Serializable object
  try {
    ProductInfo prod = (ProductInfo)e.nextElement();
    }
    catch( ClassCastException e) {
    }
 }

}

Avatar of meming
meming

You need to be sure the getAllProducts only returns objects of type ProductInfo. Otherwise, use the following code to avoid the problem.

doPost( ...., ,.....)
{
  Vector v = ejb.getAllProducts();
  Enumeration e = v.elements();
  while (e.hasMoreElements())
  {
    Object obj = e.nextElement();
    try
    {
      if (obj instanceof ProductInfo)
      {
        ProductInfo prod =
              (ProductInfo)obj;
        // use prod as ProductInfo
      }
      else
      {
        // v contains objects other than
        // ProductInfo
      }
    }
    catch( ClassCastException e)
    {}
  }
}


well see this nice remedy courtesy ,Bruce Eckel from TiJ.

Making a type-conscious Vector

You might not want to give up on this issue just yet. A more ironclad solution is to create a new class using the Vector, such that it will accept only your type and produce only your type:

//: GopherVector.java
// A type-conscious Vector
import java.util.*;

class Gopher {
  private int gopherNumber;
  Gopher(int i) {
    gopherNumber = i;
  }
  void print(String msg) {
    if(msg != null) System.out.println(msg);
    System.out.println(
      "Gopher number " + gopherNumber);
  }
}

class GopherTrap {
  static void caughtYa(Gopher g) {
    g.print("Caught one!");
  }
}

class GopherVector {
  private Vector v = new Vector();
  public void addElement(Gopher m) {
    v.addElement(m);
  }
  public Gopher elementAt(int index) {
    return (Gopher)v.elementAt(index);
  }
  public int size() { return v.size(); }
  public static void main(String[] args) {
    GopherVector gophers = new GopherVector();
    for(int i = 0; i < 3; i++)
      gophers.addElement(new Gopher(i));
    for(int i = 0; i < gophers.size(); i++)
      GopherTrap.caughtYa(gophers.elementAt(i));
  }
} ///:~
Avatar of msmolyak

ASKER

Well, thank you for an answer, but this is a liitle bit more involved.

The servlet is talking to an WebLogic App. Server and the vector of ProductInfo elements comes from a session bean.  The vector does contain the objects of type ProductInfo, I checked that.  The problem seems to be in the definition of the ProductInfo class.

This class belongs to a JAR file where all the beans are. If I do not put this class to the place where servlet can see it I get a ClassNotFoundException. If I make it available to servlet, I get a ClassCastException. Looks like there is some disconnect between ProductInfo defined on the server and on the client sides. What is interesting, the similar client whichis a Java application and not a servlet, works fine. It is the servlet which has problems.

Sorry for the confusion.
msmolyak, didn't you ask about the same question earlier as a separate item?

If it's a CORBA client/server type of app, there is a chance that the client stubs are not represented/typed correctly.

Could your debugger tell the exact type of the object before casting? Could there be multiple ProductInfo types among the imports you have?
Well, not quite. In the previous situation I had the client working if the class files for non-remote objects were separately available both on client and the server. My question was how to avoid copying them to the client and load dynamically instead.

In this case the servlet simply does not work. It is not a CORBA application.  It is a servlet to EJB application.  I know that the type is correct (again, I've printed the object's type and it is ProductInfo).  I do not have multiple ProductInfo types. The issue must have something to do with class definition coming from different class loaders, though I do not understand why the same logic works in a client application and does not work in a client servlet.

The remote objects (the session beans) get accessed correctly in the servlet, but when it tries to deal with the non-remote (serializable) object, I get this error.
ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi msmolayk:-

 Check the class with same as servlet name is present in your class path.
It's only the reason. Some class with the same name of your servlet is present in your classpath.

Hi Amit:-

 What is the title of the book?

Hi msmolyak,

Forget my pervious commnet.
It's for servlet cast exception.
I didn't read your question properly.

Any how your servlet is running.
and the solution is mbormann comment of not reloaded class.

Best of luck
ravi,
look http://www.servlets.com/

msmolyak,
no response from u?
I will try to put the class in question into the server classpath. Thank you for the detailed response.

Michael
Worked like a charm. Thanks a lot.
try
{
      have u brought the book or planning to ?
}
catch (MoreExceptionsLaterOn   oooh)
{
      ask again at EE
}

finally
{
      am shutting up
}

:-)
Still planning to. Can you also recommend a book on JSP?
I personally use this site when I want to browse for books & convince my boss for buying them , and I look at O'Reilly 's site

http://www.apl.jhu.edu/~hall/java/Java-Books.html

but see this
http://java.about.com/compute/java/library/zbookreviewz/bkr_1861002777_3463.htm

I have not worked on JSP / EJB so ...
Thanks!