Tôi đang cố gắng sắp xếp một đối tượng và gửi nó qua HTTP. Tôi đang sử dụng một vài hướng dẫn như hầu hết đối phó với ổ cắm nhưng tôi không thể sử dụng ổ cắm cho điều này, hoặc với một tập tin được lưu trữ cục bộ.Nối tiếp qua HTTP đúng cách để chuyển đổi đối tượng.
Đây là Employee lớp thử nghiệm:
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;
}
}
Tôi nghĩ đúng thats. Nhưng tôi đang mắc kẹt trên các máy chủ kết thúc, tôi có lớp lao động ở phía máy chủ quá:
public void doPost (HttpServletRequest req, HttpServletResponse resp) throws IOException {
Object obj;
ObjectInputStream objIn = new ObjectInputStream(req.getInputStream());
try {
obj = objIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Employee e = obj;
}
Làm cách nào để chuyển đối tượng này trở lại đối tượng lớp nhân viên?
Bất kỳ trợ giúp nào được đánh giá cao!