Tuesday, 15 July 2014

java - No errors but object via HTTP not working? -



java - No errors but object via HTTP not working? -

follow on before question here. trying send object employee via http. i'm not getting errors hoping printout of employee details @ other end isn't happening. i'm opening log files see printout on tomcat server other indication method has started showing start printout i'm not getting end one. isn't working right in section.

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 { system.out.println("start"); object obj; employee emp = null; objectinputstream objin = new objectinputstream(req.getinputstream()); seek { obj = objin.readobject(); } grab (classnotfoundexception e) { e.printstacktrace(); } seek { emp = (employee)objin.readobject(); } grab (classnotfoundexception e) { e.printstacktrace(); } system.out.println("end"); system.out.println(emp.name); }

any ideas whats going wrong on receiving end?

try { obj = objin.readobject(); } grab (classnotfoundexception e) { e.printstacktrace(); } seek { emp = (employee)objin.readobject(); } grab (classnotfoundexception e) { e.printstacktrace(); }

you sending 1 object , trying receive two. either need this:

obj = objin.readobject(); if (obj instanceof employee) { employee emp = (employee)obj; }

or this:

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

not mixture of both. 2 readobject() calls implies reading stream 2 distinct objects, , aren't sending them.

secondly, shouldn't grab exception , utilize instanceof on exception object. in case should have separate catch (eofexception exc), ok if expect receive 0 objects, not otherwise, , grab other possible exceptions separately: not ok.

java http serialization

No comments:

Post a Comment