tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re:[OT] Serialization
Date Mon, 04 Oct 2010 17:31:12 GMT
Hash: SHA1


(Marking off-topic because this is a Java/JSP issue and not
Tomcat-related. See below for responses.)

On 10/4/2010 9:10 AM, Wolfgang Orthuber wrote:
> my tomcat version is 5.5.17


> then all works fine, but if I use only read (on an formerly written
> file) with the same code included in another program module, I got the
> exceptions like this:
> java.lang.ClassCastException: org.apache.jsp.w.w4a_jsp$1ddm2 cannot be
> cast to org.apache.jsp.w.w4b_jsp$1ddm2

That funny error message occurs because you are trying to write one
class object and read it in as another class object. You should read-up
on serialization for more information:

> in which w4a.jsp and w4b.jsp are two different modules which include the
> same code for read and write.

That means that they are different classes, and are therefore
incompatible. You should use a class defined in a .java file and not in
a .jsp file.

Your other option is to provide customized serialization that can read
and write data without relying on Java's serialization mechanism (which
mandates that the classes be the same in order to work).

> The name of the program module is stored
> in the serialized object, but the name of the program module does not
> matter, because both modules include the same code.

No, the fact that they are the same code is irrelevant. Only the data
and the metadata are relevant, and the metadata doesn't match between
these two distinct classes.

> Do you know a simple solution which avoids the exception?

Use the same class for both reading and writing.

> The code section with read and write:

Is this inside a .jsp? If so, create a separate class. What are you
doing putting Java code into a JSP file, anyway? More comments below...

> class dm5t implements Serializable {
>     public ArrayList<ddm2>    v5;
>     public dm5t () {  v5 = new ArrayList<ddm2> (); }
>     public String topicpath(){return
> getServletContext().getRealPath("")+"/tp/";}

Note that getRealPath isn't guaranteed to return a non-null value: a
filesystem isn't guaranteed by the servlet specification, but temporary
storage is guaranteed using the "temp dir". See the servlet spec for

>     public synchronized boolean write () {

Why synchronized?

>         String fn=fntopics;
>         boolean ok=true;
>         try {
>             String spath = topicpath();
>             FileOutputStream    fs = new FileOutputStream (spath+fn);
>             ObjectOutputStream    os = new ObjectOutputStream    (fs);
>             os.writeObject (v5);
>             os.close ();}
>         catch (IOException e) {ok=false;} return ok;}
>     public synchronized boolean    read () {
>         String fn=fntopics;
>         boolean ok=true;
>         ArrayList<ddm2>    v5tmp=null;
>         try {
>             String spath = topicpath();
>             FileInputStream        fs = new FileInputStream (spath+fn);
>             ObjectInputStream    os = new ObjectInputStream    (fs);
>             v5tmp = (ArrayList<ddm2>) os.readObject ();
>             os.close ();
>         } catch (IOException e) {ok=false;}
>         catch (ClassNotFoundException e) {ok=false;}
>         if (ok)    if (v5tmp != null) v5=v5tmp;
>         return ok;}
> }

Why do you catch exceptions and return error codes? You like C-style
return codes instead of the elegance and clarity of exceptions?

- -chris
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message