enter code here使用此方法序列化为String对象:
private static String toString( Serializable o ) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return new String( Base64Coder.encode( baos.toByteArray() ) );
}然后将此String放在inputHidden html标记中,以便在HTML表单中传递。
现在在Servlet中,使用此方法将字符串反序列化为对象:
private static Object fromString( String s ) throws IOException , ClassNotFoundException {
byte [] data = Base64Coder.decode( s );
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return o;
}