Java Forum / General / December 2005
Serialize Applet to a web server via Servlet Problem
pcouas - 22 Dec 2005 17:24 GMT 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 "); } }
Roedy Green - 22 Dec 2005 22:13 GMT >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.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
pcouas - 23 Dec 2005 07:52 GMT 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 - 23 Dec 2005 08:53 GMT It seems HttpConnection flow is cutted or formated for 2062 bytes ???
Roedy Green - 23 Dec 2005 12:45 GMT >It seems HttpConnection flow is cutted or formated for 2062 bytes ??? are you using readAvailable? See http://mindprod.com/jgloss/readeverything.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
pcouas - 23 Dec 2005 13:17 GMT 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 - 23 Dec 2005 19:29 GMT 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
Roedy Green - 23 Dec 2005 22:17 GMT >I can use my socket from application, but from IE my WriteObject has an >Error [quoted text clipped - 7 lines] >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.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 23 Dec 2005 22:19 GMT >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.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
pcouas - 24 Dec 2005 07:08 GMT 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
Thomas Hawtin - 24 Dec 2005 09:12 GMT > 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
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Paulus de Boska - 24 Dec 2005 14:21 GMT 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
pcouas - 28 Dec 2005 08:55 GMT 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 - 24 Dec 2005 07:21 GMT 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 - 24 Dec 2005 08:02 GMT I have tried, that's Work and my CustomerPropertyEditor too I have saved each component in a séparate byteArray File
Youppi, Philippe
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|