自定义的测试类
public class aData implements Serializable{
int d=100;
public aData() {
}
public int getD()
{
return this.d;
}
}
Applet 端程序
try {
URL url = new URL("http://127.0.0.1:8080/ServletTest");
URLConnection urlcon = url.openConnection();
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
urlcon.setUseCaches(false);
urlcon.setRequestProperty("Content-Type",
"application/x-java-serialized-object");
OutputStream out = urlcon.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(new aData());
oos.flush();
oos.close();
urlcon.getHeaderField(0);//这一句非常重要,如果没有此句服务器端没反应
} catch (Exception ex) {
ex.printStackTrace();
}
Servlet端程序
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.servlet.ServletException;
public class ServletTest extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
//Initialize global variables
public void init() throws ServletException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
ObjectInputStream oin = new ObjectInputStream(req.getInputStream());
aData vo = null;
try {
vo = (aData) oin.readObject();
oin.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("============" + vo.getD());
}
public void doGet(HttpServletRequest req, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("application/x-java-serialized-object");
doPost(req, response);
}
//Clean up resources
public void destroy() {
}
}