Serialize Applet to a web server via Servlet Problem

Discussion in 'Java' started by pcouas, Dec 22, 2005.

  1. pcouas

    pcouas Guest

    Hi,

    I try to Serialise an Applet on my WebServer, i convert Object to
    ByteArray, before sending it to webserver.
    There is 2050 bytes in byte array on my applet before sending it, and
    2062 bytes in byte array on tomcat !

    Where is my mistake ?
    Regards
    Philippe



    Applet
    void WriteApplet(String filename)
    {
    ObjectOutputStream out;
    System.out.println("Debut Test WRITE APPLET...");

    //Serialisation de la frame
    try
    {
    String m_URL=new
    String("http://172.16.0.101:8080/test/servlet/servletgraph.XXServlet");
    String string4=m_URL;
    System.out.println("w01 "+string4);

    URL u=new URL(string4);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(this);
    oos.flush();
    oos.close();
    byte b[] = baos.toByteArray();

    HttpURLConnection urlc = (java.net.HttpURLConnection)
    u.openConnection();
    // Set the Post (true) or Get (false) method.
    urlc.setDoOutput(true);
    urlc.setDoInput(true);
    urlc.setUseCaches(false);
    urlc.setAllowUserInteraction(false);

    urlc.setRequestProperty("Content-Type",
    "application/octet-stream");

    String lengthString = String.valueOf(baos.size());
    urlc.setRequestProperty("Content-Length", lengthString);
    System.out.println("Object is avant ecriture " + lengthString + "
    bytes");

    out = new ObjectOutputStream(urlc.getOutputStream());
    out.write(b);

    if (urlc.getResponseCode() != HttpURLConnection.HTTP_OK) {
    System.out.println("Error...");
    } else {
    System.out.println("I have now access to the stream...");
    // blaha balaaha blaahahaha
    }

    out.flush();
    out.close();
    System.out.println("Fin Test WRITE APPLET...");
    }
    catch (IOException ioe) {
    System.err.println(ioe);
    ioe.printStackTrace();
    }
    }

    -------------------------------------------------------------------------

    Servlet
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    System.out.println("XXServlet doPost X1 "+new Date());
    boolean debug=true;

    InputStream in = req.getInputStream();
    byte[] bufimage=null;
    bufimage = new byte[4 * 1024]; // 4K buffer
    int len;
    RandomAccessFile f = new
    java.io.RandomAccessFile(req.getRealPath("/")+"xxservlet", "rw");
    int size = req.getIntHeader("content-length");
    bufimage = new byte[size]; // 4K buffer
    if(debug) System.out.println("XXServlet bytearray02 "+size);
    int nb=0;
    while ((len = in.read(bufimage, 0, bufimage.length)) != -1)
    {
    if(debug) System.out.println("XXServlet bytearray03
    content-length "+len);
    f.write(bufimage, 0, len); //Fichier
    nb=+len;

    }
    if(debug) System.out.println("XXServlet bytearray05 "+nb);

    f.close();

    if(debug) System.out.println("XXServlet write04 ");


    }
    }
    pcouas, Dec 22, 2005
    #1
    1. Advertising

  2. pcouas

    Roedy Green Guest

    On 22 Dec 2005 09:24:01 -0800, "pcouas" <> wrote,
    quoted or indirectly quoted someone who said :

    >There is 2050 bytes in byte array on my applet before sending it, and
    >2062 bytes in byte array on tomcat !


    Why not dump it at both ends to see the differences.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 22, 2005
    #2
    1. Advertising

  3. pcouas

    pcouas Guest

    Hi,


    My Java Applet Code give me Object is avant ecriture 2383 bytes
    String lengthString = String.valueOf(baos.size());



    And when i use EtherALL for viewing flow, i have 2062 ???
    POST /infodev/servlet/servletgraph.XXServlet HTTP/1.1

    Content-Type: application/octet-stream

    Method: POST

    Content-Length: 2062

    User-Agent: Java1.3.1

    Host: 172.16.0.101:8080

    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

    Connection: keep-alive



    .....z........sr..applet8.Applet6..s.a
    ......L..button1t..Ljava/awt/Button;xr..java.applet.Applet..[=.&.....L..accessibleContextt.'Ljavax/accessibility/AccessibleContext;xr..java.awt.Panel.$*1.......xr..java.awt.Container@..s...'...I..containerSerializedDataVersionI..ncomponents[..componentt..[Ljava/awt/Component;L.
    dispatchert.
    Ljava/awt/LightweightDispatcher;L..layoutMgrt..Ljava/awt/LayoutManager;L..maxSizet..Ljava/awt/Dimension;xr..java.awt.Component...Y.<.....I..componentSerializedDataVersionZ..enabledJ..eventMaskZ..hasFocusI..heightZ..isPackedZ..nameExplicitlySetZ.
    newEventsOnlyZ..validZ..visibleI..widthI..xI..yL..accessibleContextq.~..L.
    backgroundt..Ljava/awt/Color;L.
    changeSupportt."Ljava/beans/PropertyChangeSupport;L..cursort..Ljava/awt/Cursor;L.
    dropTargett..Ljava/awt/dnd/DropTarget;L..fontt..Ljava/awt/Font;L.
    foregroundq.~..L..localet..Ljava/util/Locale;L..minSizeq.~..L..namet..Ljava/lang/String;L..peerFontq.~..L..popupst..Ljava/util/Vector;L..prefSizeq.~..xp..........................$........ppppppppppsr.
    jaz....va.awt.Font..5...Vs...I..fontSerializedDataVersionF..pointSizeI..sizeI..styleL..fRequestedAttributest..Ljava/util/Hashtable;L..nameq.~..xp....A@..........sr..java.util.Hashtable...%!J.....F.
    loadFactorI..thresholdxp?fff....w.........sr..java.awt.font.TextAttributekx..
    ..
    F...xr./java.text.AttributedCharacterIterator$Attribute..t&.G.\...L..nameq.~..xpt..familyt..Dialogsq.~..t..transformsr.
    java.awt.font.TransformAttribute...0...Z...L..transformt..Ljava/awt/geom/AffineTransform;xpsr..java.awt.geom.AffineTransform.x..J..b...D..m00D..m01D..m02D..m10D..m11D..m12xp?...............................?...............xsq.~..t..sizesr..java.lang.Float.....<.....F..valuexr..java.lang.Number...........xpA@..xq.~..xpppsr..java.awt.ComponentOrientation...E..c....I..orientationxp....px........ur..[Ljava.awt.Component;...
    ....u...xp....sr..java.awt.Button.:..x.S....I..buttonSerializedDataVersionL.
    actionCommandq.~..L..labelq.~..xq.~.
    ...........................I.......Apsr..java.awt.Color......3u...F..falphaI..valueL..cst..Ljava/aHTTP/1.1
    200 OK

    Content-Length: 0

    Date: Fri, 23 Dec 2005 07:35:36 GMT

    Server: Apache Tomcat/4.1.30 (HTTP/1.1 Connector)



    Regards
    Philippe
    pcouas, Dec 23, 2005
    #3
  4. pcouas

    pcouas Guest

    It seems HttpConnection flow is cutted or formated for 2062 bytes ???
    pcouas, Dec 23, 2005
    #4
  5. pcouas

    Roedy Green Guest

    On 23 Dec 2005 00:53:13 -0800, "pcouas" <> wrote,
    quoted or indirectly quoted someone who said :

    >It seems HttpConnection flow is cutted or formated for 2062 bytes ???


    are you using readAvailable? See
    http://mindprod.com/jgloss/readeverything.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 23, 2005
    #5
  6. pcouas

    pcouas Guest

    No, i have created a Socket on port 8080 with host 172.16.0.101, and
    that's ok
    Flow is not truncated

    Regards
    Philippe
    pcouas, Dec 23, 2005
    #6
  7. pcouas

    pcouas Guest

    Hi,

    I can use my socket from application, but from IE my WriteObject has an
    Error
    How could i found my Error .
    Same Program could running from my IDE, but not from IE
    Why ?

    Regards
    Philippe

    Erreur java.awt.EventQueue
    java.io.NotSerializableException: java.awt.EventQueue
    at java.io_ObjectOutputStream.writeObject0(Unknown Source)
    at java.io_ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io_ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io_ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io_ObjectOutputStream.writeObject0(Unknown Source)
    at java.io_ObjectOutputStream.writeArray(Unknown Source)
    at java.io_ObjectOutputStream.writeObject0(Unknown Source)
    at java.io_ObjectOutputStream.access$100(Unknown Source)
    at java.io_ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
    at java.io_ObjectOutputStream.writeFields(Unknown Source)
    at java.awt.Container.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io_ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io_ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io_ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io_ObjectOutputStream.writeObject0(Unknown Source)
    at java.io_ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io_ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io_ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io_ObjectOutputStream.writeObject0(Unknown Source)
    at java.io_ObjectOutputStream.writeObject(Unknown Source)
    at applet8.Applet8e6.WritePostObjectSocket(Applet8e6.java:396)
    at applet8.Applet8e6.WriteApplet(Applet8e6.java:375)
    at
    applet8.Applet8e6_this_mouseAdapter.mousePressed(Applet8e6.java:952)
    at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
    Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Sourc
    pcouas, Dec 23, 2005
    #7
  8. pcouas

    Roedy Green Guest

    On 23 Dec 2005 11:29:04 -0800, "pcouas" <> wrote,
    quoted or indirectly quoted someone who said :

    >I can use my socket from application, but from IE my WriteObject has an
    >Error
    >How could i found my Error .
    >Same Program could running from my IDE, but not from IE
    >Why ?
    >
    >Regards
    >Philippe
    >
    >Erreur java.awt.EventQueue
    >java.io.NotSerializableException


    It says you tried to write out some object that did not implement the
    Serializable interface. You can't write objects to a file in an
    Applet without signing the Applet.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 23, 2005
    #8
  9. pcouas

    Roedy Green Guest

    On 23 Dec 2005 11:29:04 -0800, "pcouas" <> wrote,
    quoted or indirectly quoted someone who said :

    >at applet8.Applet8e6.WriteApplet(Applet8e6.java:375)


    looks like you tried to serialise the entire applet. That means the
    whole thing must be serializable. Try writing out just the pieces you
    need.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 23, 2005
    #9
  10. pcouas

    pcouas Guest

    Yes i tried to Serialize all Applet, I could serialize it when i launch
    my program like an Application, but i not from Internet Explorer.

    Regards
    Philippe
    pcouas, Dec 24, 2005
    #10
  11. pcouas

    pcouas Guest

    Ok , next week i implement an Writing method only for Each Component in
    Frame Applet and not Applet itself.
    My applet is signed

    Regards
    Philippe
    pcouas, Dec 24, 2005
    #11
  12. pcouas

    pcouas Guest

    I have tried, that's Work and my CustomerPropertyEditor too
    I have saved each component in a s├ęparate byteArray File


    Youppi, Philippe
    pcouas, Dec 24, 2005
    #12
  13. pcouas wrote:
    > Yes i tried to Serialize all Applet, I could serialize it when i launch
    > my program like an Application, but i not from Internet Explorer.


    Does it make any sense to serialise an applet? When you deserialise,
    what are you going to do with it? As far as I can see, you can't go and
    put it back into a page.

    Although it was all the rage in 1997, I don't think serialising
    components is a particularly useful thing to do. Letting the data go
    outside of the current JVM instance makes it much more difficult to
    change the code, and breaks on Swing version changes. I guess you might
    want to keep hold of the state of lots of hidden screens within an
    application, or perhaps help with in place upgrading.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Dec 24, 2005
    #13
  14. As Roedy pointed out, you're code,in a jar, has to be signed if you
    want to be able to write from an applet. Furthermore, don't know if
    this helps, but I noticed this in your doPost method :
    nb=+len;
    where you probably meant :
    nb += len ;

    ---
    Paul Hamaker, SEMM
    http://javalessons.com
    Paulus de Boska, Dec 24, 2005
    #14
  15. pcouas

    pcouas Guest

    I have tried, that's Work and my CustomerPropertyEditor too
    I have saved each component in a s├ęparate byteArray File


    My problem is resolved Thanks
    Youppi, Philippe
    pcouas, Dec 28, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordz
    Replies:
    3
    Views:
    3,933
    John Oakes
    Jun 7, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    1,996
    Jack Jia
    Apr 4, 2004
  3. Roland Poellinger
    Replies:
    1
    Views:
    1,298
    Ryan Dillon
    Mar 12, 2005
  4. Tedy
    Replies:
    1
    Views:
    448
    Andrew Thompson
    Aug 4, 2005
  5. pcouas
    Replies:
    0
    Views:
    742
    pcouas
    Dec 16, 2005
Loading...

Share This Page