服务器端:
@RequestMapping("/obtainUser")
public voidobtainUser(Integer uid, HttpServletResponse response)
throws IOException {
//假设一用户实例
User user = userService.get(uid);
ObjectOutputStream objstream = new ObjectOutputStream(response.getOutputStream());
objstream.writeObject(user);
objstream.flush();
}
applet端:
使用这种方式时,需要将远程获取的POJO类一同打包
private User obtainRemoteUser(Integer uid){
URL url = new URL(getCodeBase(),"/obtainUser?uid=" + uid + "");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("Cookie", "JSESSIONID="+sid); //使用firefox时会使用session丢失,对于一些有登录要求的系统将不能获取远程对象
ObjectInputStream objstream = new ObjectInputStream(conn.getInputStream());
User user= (User) objstream.readObject();
return user;
}