Newsgroup: it.comp.java
Titolo: Serialization Question - Why is this class not serializing all of its objects?

Hi,
Following code works as it is supposed to work however, Serialization does
not.
When I unserialize the class - I don' get my objects back.
Help,
Manish Namdeo
Sr. Programmer
Design Trust Inc.,
/*
 * RoomManager.java
 *
 * Created on December 27, 2000, 9:12 AM
 */
/* Note -
 * This servlet will receive a room name & action.
 * If action is add, room will be added to Rooms object. Then a HTML Select
List of Rooms will be sent back.
 * If action is delete, room will be deleted.
 * If action is join, user will be made a member of that room.
 */
// package work;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import work.Room;
/**
 *
 * @author  manishn
 * @version
 */
public class RoomManager extends HttpServlet implements Serializable{
    public Hashtable ChatRooms = new Hashtable();
    static boolean debug = true;
    /** Initializes the servlet.
    */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        RoomManager thisRoomManager = new RoomManager();
        System.out.println("Unserializing RoomManager");
        try {
                FileInputStream fin     = new
FileInputStream("/tmp/RoomManager.dat");
                ObjectInputStream ois   = new ObjectInputStream(fin);
                thisRoomManager = (RoomManager) ois.readObject();
                ois.close();
            }
            catch (Exception
                  e.printStackTrace();
            }
    }
    /** Destroys the servlet.
    */
    public void destroy() {
        RoomManager thisRoomManager = new RoomManager();
        System.out.println("Serializing RoomManager");
        try {
                FileOutputStream fout  = new
FileOutputStream("/tmp/RoomManager.dat");
                ObjectOutputStream oos = new ObjectOutputStream(fout);
                oos.writeObject(thisRoomManager);
                oos.close();
        }
        catch (Exception
            e.printStackTrace();
        }
        thisRoomManager = null;
    }
    /** Processes requests for both HTTP <code>GET</code> and
<code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, java.io.IOException {
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
        String ChatRoom, Action;
        StringBuffer page = new StringBuffer("");
        ChatRoom    = request.getParameter("chatroom");     /* Room to be
added or deleted. */
        Action      = request.getParameter("action");       /* Action to be
performed. */
        if (this.debug){
            System.out.println("Chat room:" + ChatRoom + " Action:" +
Action);
        }
        /* Generic header. */
        page.append("<html>");
        page.append("<head>");
        page.append("\n<title>RoomManager Servlet</title>");
        page.append("</head>");
        page.append("\n<body>");
        page.append("\n<form name=\"Rooms\" method=\"get\">");
        page.append("\n<table border=0 cellspacing=0 cellpadding=0>");
        page.append("\n\t<tr>\n\t\t<td>");
        page.append("\nChat rooms:");
        page.append("\n</td>");
        page.append("\n</tr>");
        page.append("\n\t<tr>\n\t\t<td align=right>");
        page.append("\n<select name=\"allrooms\" size=\"5\">");
        if ((ChatRoom != null) && (Action != null)){    /* If both are not
null then just return the list of rooms, else process it. */
                if (Action.equals("add")){
                    Room thatRoom = (Room) ChatRooms.get(ChatRoom);
                    if (thatRoom == null){ /* Room does not exist. */
                            Room thisRoom = new Room(ChatRoom, "Manish
Namdeo");
                            ChatRooms.put(ChatRoom, thisRoom);
                            if (this.debug){
                                System.out.println("Chat room added:" +
ChatRoom);
                            }
                        }
                    else{
                            thatRoom = null; /* Destroy the Room object. */
                    }
                }
                if (Action.equals("delete")){
                    Room thatRoom = (Room) ChatRooms.get(ChatRoom);
                    if (thatRoom != null){          /* Room does exist. */
                        ChatRooms.remove(ChatRoom); /* Delete the Room. */
                        thatRoom = null;            /* Destroy the Room
object. */
                    }
                }
                if (Action.equals("join")){
                }
        }
        /* output your page here */
        Enumeration AllRooms = ChatRooms.elements();
        while(AllRooms.hasMoreElements()){
             Room thisRoom = (Room) AllRooms.nextElement();
             page.append("\n\t<option value=\"" + thisRoom.strChatRoom +
"\">" + thisRoom.strChatRoom);
        }
        /* Generic footer. */
        page.append("</select>");
        page.append("\n</td>");
        page.append("\n</tr>");
        page.append("\n\t<tr>\n\t\t<td>");
        page.append("\nRoom:");
        page.append("\n</td>");
        page.append("\n</tr>");
        page.append("\n\t<tr>\n\t\t<td>");
        page.append("\n<input type=text name=chatroom size=20>");
        page.append("\n</td>");
        page.append("\n</tr>");
        page.append("\n\t<tr>\n\t\t<td align=right>");
        page.append("\n<input type=submit name=action value=add>");
        page.append("\n</td>");
        page.append("\n</tr>");
        page.append("\n</table>");
        page.append("\n</form>");
        page.append("</body>");
        page.append("</html>");
        out.println(page.toString());
        out.close();
    }
    /** Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse
response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
    /** Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse
response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
    /** Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Chat Application: Room manager servlet.";
    }
}

Why are you serializing 'thisRoomManager'?  I would think you wanted to
serialize ChatRooms (also, you may want to name the field chatRooms to comply
better with the Java naming conventions.  See the Java Language spec).
It is appearent that in 'destroy()' you are instantiating a new RoomManager,
and then serializing it.  It has no state in it - or at least having anything
to do with you processRequest().
Also, be sure work.Room is serializable in case you meant to serialize
ChatRooms.
And, during deserialization you don't need to instantiate a RoomManager since
you're going to get one a few lines later.
RoomManager thisRoomManager = new RoomManager(); <<-- no
RoomManager thisRoomManager=null; <<-- ok.
