Reputation: 8717
Follow on from an earlier question here. Trying to send this object Employee via HTTP. I'm not getting any errors but am hoping for a printout of the employee details at the other end but something isn't happening. I'm opening my log files to see the printout on my tomcat server but other than the indication that the method has started showing the START printout I'm not getting the END one. So something isn't working right in that section.
Here is the 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 a check to " + 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;
try {
// 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();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
// recieve reply
try {
ObjectInputStream objIn = new ObjectInputStream(conn.getInputStream());
reply = objIn.readObject();
objIn.close();
} catch (Exception ex) {
// it is ok if we get an exception here
// that means that there is no object being returned
System.out.println("No Object Returned");
if (!(ex instanceof EOFException))
ex.printStackTrace();
System.err.println("*");
}
return reply;
}
}
I think thats correct. But I'm stuck on the server end, I have the employee class on the 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());
try {
obj = objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
emp = (Employee)objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("END");
System.out.println(emp.name);
}
Any ideas whats going wrong on the receiving end?
Upvotes: 0
Views: 66
Reputation: 310998
try {
obj = objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
emp = (Employee)objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
You are sending one object and trying to receive two. You either need this:
obj = objIn.readObject();
if (obj instanceof Employee)
{
Employee emp = (Employee)obj;
}
or this:
Employee emp = (Employee)objIn.readObject();
Not a mixture of both. Two readObject()
calls implies reading the stream for two distinct objects, and you aren't sending them.
Secondly, you shouldn't catch Exception
and then use instanceof
on the exception object. In this case you should have a separate catch (EOFException exc)
, which is OK if you expect to receive zero objects, but not otherwise, and then catch the other possible exceptions separately: they are not OK.
Upvotes: 1