reset password
Author Message
Vanquish39
Posts: 134
Posted 23:12 Apr 12, 2011 |

Professor, if you pass an object through a session, the next time the server restarts and the servlet is reloaded, is that object gone?

For instance, when a user accesses my servlet for the first time, he goes through a form to enter firstname, lastname, and age.  Then this Person(firstname, lastname, age) object is added to the session.  If i reload the page, it updates the visitCount.  But if I restart the server and re-implement the servlet, the object becomes null.  Why?

I have attached the code.  Open it with Notepad++

Attachments:
Last edited by Vanquish39 at 23:13 Apr 12, 2011.
abajpai
Posts: 52
Posted 23:17 Apr 12, 2011 |

I think this is the part where the databases come in. I would say it is because when you stop/restart the server, it flushes the memory caches as well.

Vanquish39
Posts: 134
Posted 23:21 Apr 12, 2011 |

Integers work though, and they are objects.  Strange.

Last edited by Vanquish39 at 23:22 Apr 12, 2011.
abajpai
Posts: 52
Posted 23:25 Apr 12, 2011 |

Maybe I'm a bit confused here. Did you mean that the value of integer objects are retained after you restart the server?

Last edited by abajpai at 23:27 Apr 12, 2011.
Vanquish39
Posts: 134
Posted 23:27 Apr 12, 2011 |

Integer objects do not turn NULL, but the person objects turn NULL.

Last edited by Vanquish39 at 23:29 Apr 12, 2011.
abajpai
Posts: 52
Posted 23:30 Apr 12, 2011 |

That's strange. All the objects on your servlet are supposed to be reset to their initial values if you restart the server.

Last edited by abajpai at 23:32 Apr 12, 2011.
Vanquish39
Posts: 134
Posted 23:33 Apr 12, 2011 |

yea, check out this code.  No matter what I do, the Integer object never turns to NULL they just keep incrementing.

Attachments:
Last edited by Vanquish39 at 23:37 Apr 12, 2011.
abajpai
Posts: 52
Posted 23:39 Apr 12, 2011 |

.

Last edited by abajpai at 23:40 Apr 12, 2011.
Vanquish39
Posts: 134
Posted 23:40 Apr 12, 2011 |

Shows the session information including the visit count.  The visit count always increments, even if I restart the server.  It never turns to 0.

abajpai
Posts: 52
Posted 23:40 Apr 12, 2011 |

did you try restarting your browser?

Vanquish39
Posts: 134
Posted 23:40 Apr 12, 2011 |

ya

abajpai
Posts: 52
Posted 23:43 Apr 12, 2011 |

i'm guessing your cookies are not expiring but it should at the end of the session which is when you shut down your browser. check the expiry header on your cookie for this page. maybe that will be of some help

Last edited by abajpai at 23:46 Apr 12, 2011.
cysun
Posts: 2935
Posted 10:23 Apr 13, 2011 |

Tomcat tries to preserve live sessions between restarts. This feature is useful when you want to restart the server to load a new version of an application, but there are some users who are currently logged in. By preserving sessions, once the server is restarted, these users don't have to log in again (and hopefully won't notice the service interruption).

The way Tomcat saves session data is to write them to disk. Note that this requires the objects to implement the Serializable interface, which I guess is the reason why Integer was save but not the Person object in your example.