Wednesday, 15 August 2012

java - Serializing over HTTP correct way to convert object. -



java - Serializing over HTTP correct way to convert object. -

i'm trying serialize object , send on http. i'm using few tutorials deal sockets can't utilize sockets this, or file been stored locally.

here test class employee:

public class employee implements java.io.serializable { public string name; public string address; public transient int ssn; public int number; public void mailcheck() { system.out.println("mailing check " + name + " " + address); } }

client side:

public class serializeandsend {

public static void main(string args[]){ one.employee e = new one.employee(); e.name = "reyan ali"; e.address = "phokka kuan, ambehta peer"; e.ssn = 11122333; e.number = 101; sendobject(e); } public static object sendobject(object obj) { urlconnection conn = null; object reply = null; seek { // open url connection url url = new url("///myurl///"); conn = url.openconnection(); conn.setdoinput(true); conn.setdooutput(true); conn.setusecaches(false); // send object objectoutputstream objout = new objectoutputstream(conn.getoutputstream()); objout.writeobject(obj); objout.flush(); objout.close(); } grab (ioexception ex) { ex.printstacktrace(); homecoming null; } // recieve reply seek { objectinputstream objin = new objectinputstream(conn.getinputstream()); reply = objin.readobject(); objin.close(); } grab (exception ex) { // ok if exception here // means there no object beingness returned system.out.println("no object returned"); if (!(ex instanceof eofexception)) ex.printstacktrace(); system.err.println("*"); } homecoming reply; }

}

i think thats correct. i'm stuck on server end, have employee class on server side too:

public void dopost(httpservletrequest req, httpservletresponse resp) throws ioexception {

object obj; objectinputstream objin = new objectinputstream(req.getinputstream()); seek { obj = objin.readobject(); } grab (classnotfoundexception e) { e.printstacktrace(); } employee e = obj;

}

how turn object employee class object?

any help appreciated!

just typecast it.

employee emp = (employee)objin.readobject();

java serialization

No comments:

Post a Comment