简体   繁体   English

Servlet和Applet之间的自定义对象

[英]Custom Object Between Servlet and Applet

i have a servlet and an applet, that share a custom Object in java: 我有一个servlet和一个applet,它们在java中共享一个自定义对象:

public class Apartment  {
    public String id_apartment;
    public String user_owner;
    public String address;
}

and the servlet pass the object in this way: 然后servlet以这种方式传递对象:

OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(apartment);
oos.writeObject(apartments);
oos.flush();
oos.close();

and the applet receive it in this way: 小程序以这种方式接收它:

InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
Apartment apart = (Apartment) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();

but the applet give me an error: java.lang.ClassNotFoundException: servlet_package.Apartment , i have created the Apartment class both in the applet jar and in the servlet package, but doesn't work...how i can do it? 但是小程序给我一个错误: java.lang.ClassNotFoundException: servlet_package.Apartment ,我已经在小程序jar和servlet包中创建了Apartment类,但是不起作用...我该怎么做?

i have found this question on SO: Passing custom Objects from applet to servlet 我在SO上发现了这个问题:将自定义对象从applet传递到servlet

but i can't understand the answer... 但我不明白答案...

any help? 有什么帮助吗?

You must not create the same class, in a different package, in the servlet and in the applet. 您不得在servlet和applet中的不同包中创建相同的类。 You must create a single, unique Apartment class, compile it, and have it in the classpath of the servlet, and in the classpath of the applet. 您必须创建一个唯一的Apartment类,对其进行编译,并将其放在servlet的类路径中以及applet的类路径中。 The same class file must be shared by the two parties. 双方必须共享同一个类文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM