All data types passed between a client and a server must be serializable. That is, they must implement the java.io.Serializable interface. In the Cart example, the bean returns a list of items to the client. If there were no serializable restrictions, you could use _items.elements() to return the contents of the item vector. But _items.elements() returns a Java Enumeration object, which is not serializable. To avoid this problem, the program implements a class called com.inprise.ejb.util.VectorEnumeration(_items). This class takes a vector and returns an actual enumeration, which is serializable, for the contents of that vector. The CartBean object passes this serializable vector to the client, and receives a serializable vector passed from the client side. The getContents() method does the conversion between a Java Enumeration and a serializable VectorEnumeration. public java.util.Enumeration getContents() { System.out.println("\tgetContents(): " + this); return new com.inprise.ejb.util.VectorEnumeration(_items); } 我可不可以直接把Vector返回而不用将其转换成Enumeration类型呢?然后在客户端进行转换?
Vector 已经实现 Serializable
其实对象的序列化就是对象的保存(我认为) 下面有两个程序,一个是保存一个对象,一个是读出这个对象import java.awt.*; import javax.swing.*; import java.io.*; import java.util.*; import java.awt.event.*; /** * Title: * Description: * Copyright: Copyright (c) 2002 * Company: * @author * @version 1.0 */public class SerialSample1 extends JFrame { public SerialSample1(String title) { super(title); } public static void main(String[] args) { SerialSample1 frame1 = new SerialSample1("SerialSample1"); frame1.init(); frame1.setSize(400,400); frame1.setVisible(true); frame1.addWindowListener(frame1.a); } WindowClose a = new WindowClose(); class WindowClose extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } } DrawPicture drawArea = new DrawPicture(); JButton saveButton = new JButton("Save Picture"), clearButton = new JButton("Clear Picture"); Vector picture; ActionListener saveL = new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("objectFile")); out.writeObject(picture); out.flush(); out.close(); }catch(Exception ex){ } } }; ActionListener clearL = new ActionListener(){ public void actionPerformed(ActionEvent e){ picture = new Vector(); repaint(); } }; MouseAdapter drawL = new MouseAdapter(){ public void mouseClicked(MouseEvent e){ Point p = new Point(e.getX(),e.getY()); picture.(p); repaint(); } }; public void init(){ picture = new Vector(); saveButton.addActionListener(saveL); clearButton.addActionListener(clearL); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(saveButton); cp.add(clearButton); cp.add(drawArea); File file = new File("c:"+System.getProperty("file.separator")+"dlr.txt"); System.out.println(file.getPath()); System.out.println(file.exists()); drawArea.addMouseListener(drawL); } class DrawPicture extends JTextArea{ DrawPicture(){ super("Draw circles",10,20); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.blue); int i=0; while(i<picture.size()){ Point p0=(Point)(picture.get(i++)); int x=p0.x; int y=p0.y; g.drawOval(x,y,50,50); } } } } //恢复对象 import java.awt.*; import javax.swing.*; import java.io.*; import java.util.*; import java.awt.event.*; import javax.swing.tree.DefaultMutableTreeNode; /** * Title: * Description: * Copyright: Copyright (c) 2002 * Company: * @author * @version 1.0 */public class DeserialSample1 extends JFrame { public DeserialSample1(String title) { super(title); } public static void main(String[] args) { DeserialSample1 frame1 = new DeserialSample1("DeSerialSample1"); frame1.init(); frame1.setSize(400,400); frame1.setVisible(true); frame1.addWindowListener(frame1.a); } WindowClose a = new WindowClose(); class WindowClose extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } } DrawPicture drawArea = new DrawPicture(); Vector picture; JButton loadButton = new JButton("Load Picture"); MouseAdapter drawL = new MouseAdapter(){ public void mouseClicked(MouseEvent e){ Point p = new Point(e.getX(),e.getY()); picture.add(p); repaint(); } }; ActionListener restoreL = new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ System.out.println("ok"); ObjectInputStream in = new ObjectInputStream(new FileInputStream("objectFile")); picture = (Vector)in.readObject(); in.close(); repaint(); }catch(Exception e1){ } } }; public void init(){ picture= new Vector(); loadButton.addActionListener(restoreL); Container cp =getContentPane(); cp.setLayout(new FlowLayout()); cp.add(loadButton); cp.add(drawArea); drawArea.addMouseListener(drawL); } class DrawPicture extends JTextArea{ DrawPicture(){ super("Draw circles",10,20); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.blue); int i=0; while(i<picture.size()){ Point p0=(Point)(picture.get(i++)); int x=p0.x; int y=p0.y; g.drawOval(x,y,50,50); } } } } 直接序列化Vector没问题的
public java.util.Enumeration getContents() {
System.out.println("\tgetContents(): " + this);
return new com.inprise.ejb.util.VectorEnumeration(_items);
}
我可不可以直接把Vector返回而不用将其转换成Enumeration类型呢?然后在客户端进行转换?
Vector 已经实现 Serializable
下面有两个程序,一个是保存一个对象,一个是读出这个对象import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/public class SerialSample1 extends JFrame { public SerialSample1(String title) {
super(title);
}
public static void main(String[] args) {
SerialSample1 frame1 = new SerialSample1("SerialSample1");
frame1.init();
frame1.setSize(400,400);
frame1.setVisible(true);
frame1.addWindowListener(frame1.a);
}
WindowClose a = new WindowClose();
class WindowClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
DrawPicture drawArea = new DrawPicture();
JButton
saveButton = new JButton("Save Picture"),
clearButton = new JButton("Clear Picture");
Vector picture;
ActionListener saveL = new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("objectFile"));
out.writeObject(picture);
out.flush();
out.close();
}catch(Exception ex){
}
}
};
ActionListener clearL = new ActionListener(){
public void actionPerformed(ActionEvent e){
picture = new Vector();
repaint();
}
};
MouseAdapter drawL = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Point p = new Point(e.getX(),e.getY());
picture.(p);
repaint();
}
};
public void init(){
picture = new Vector();
saveButton.addActionListener(saveL);
clearButton.addActionListener(clearL);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(saveButton);
cp.add(clearButton);
cp.add(drawArea);
File file = new File("c:"+System.getProperty("file.separator")+"dlr.txt");
System.out.println(file.getPath());
System.out.println(file.exists());
drawArea.addMouseListener(drawL);
}
class DrawPicture extends JTextArea{
DrawPicture(){
super("Draw circles",10,20);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
int i=0;
while(i<picture.size()){
Point p0=(Point)(picture.get(i++));
int x=p0.x;
int y=p0.y;
g.drawOval(x,y,50,50);
}
}
}
}
//恢复对象
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/public class DeserialSample1 extends JFrame { public DeserialSample1(String title) {
super(title);
}
public static void main(String[] args) {
DeserialSample1 frame1 = new DeserialSample1("DeSerialSample1");
frame1.init();
frame1.setSize(400,400);
frame1.setVisible(true);
frame1.addWindowListener(frame1.a);
}
WindowClose a = new WindowClose();
class WindowClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
DrawPicture drawArea = new DrawPicture();
Vector picture;
JButton loadButton = new JButton("Load Picture");
MouseAdapter drawL = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Point p = new Point(e.getX(),e.getY());
picture.add(p);
repaint();
}
};
ActionListener restoreL = new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
System.out.println("ok");
ObjectInputStream in = new ObjectInputStream(new FileInputStream("objectFile"));
picture = (Vector)in.readObject();
in.close();
repaint();
}catch(Exception e1){
}
}
};
public void init(){
picture= new Vector();
loadButton.addActionListener(restoreL);
Container cp =getContentPane();
cp.setLayout(new FlowLayout());
cp.add(loadButton);
cp.add(drawArea);
drawArea.addMouseListener(drawL);
} class DrawPicture extends JTextArea{
DrawPicture(){
super("Draw circles",10,20);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
int i=0;
while(i<picture.size()){
Point p0=(Point)(picture.get(i++));
int x=p0.x;
int y=p0.y;
g.drawOval(x,y,50,50);
}
}
}
}
直接序列化Vector没问题的
序列化的时候就只序列化这个对象,它保存着圆的圆心坐标
第二个程序就是读出这个对象,并重新显示