没有错误,但通过HTTP的对象不起作用?

时间:2013-02-20 01:37:23

标签: java http serialization

从此前的问题开始。尝试通过HTTP发送此对象Employee。我没有收到任何错误,但我希望在另一端打印出员工的详细信息,但事情并没有发生。我打开我的日志文件以查看我的tomcat服务器上的打印输出,但除了指示该方法已开始显示START打印输出我没有得到END。所以在那一节中有些东西不能正常工作。

这是测试类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);
        }

} 

客户端:

公共类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;
}

}

我认为这是正确的。但是我被困在服务器端,我也有服务器端的员工类:

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);

}

任何想法在接收端出错了吗?

1 个答案:

答案 0 :(得分:1)

try {
    obj = objIn.readObject();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

try {
    emp = (Employee)objIn.readObject();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

您正在发送一个对象并尝试接收两个对象。你要么需要这个:

obj = objIn.readObject();
if (obj instanceof Employee)
{
  Employee emp = (Employee)obj;
}

或者这个:

Employee emp = (Employee)objIn.readObject();

不是两者的混合物。两个readObject()调用意味着为两个不同的对象读取流,而您没有发送它们。

其次,您不应该捕获Exception,然后在异常对象上使用instanceof。在这种情况下,您应该有一个单独的catch (EOFException exc),如果您希望接收零对象,则可以,但不是其他,然后单独捕获其他可能的异常:它们不正常。