0

I am editing a class that is mean to be placed into the session of a servlet use as a key for a hastable of other objects. I do not know what the minimal requirements for an object which can be placed into the HttpSession. what are the minimal requirements for an object which can be placed into the HttpSession?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jillian
  • 52
  • 6

2 Answers2

1

It should be thread-safe (or at least you should be aware that it can be used by several threads concurrently).

If you plan to save the session to disk or to share it among a cluster of servers, then it should also be Serializable.

And if that object is supposed to be used as a key of a HashMap, then it should override hashCode() and equals() properly, and it would be a good idea to make it immutable.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

All objects that are placed in a HttpSession should implement java.io.Serializable.

That's really the only "minimal" requirement.

For scalability you generally want to minimise the overall size of objects that you place in the session as well.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • Is there any other requirement other than putting the implements statement? Do I have to provide an implementation for any methods? – jillian Feb 17 '16 at 14:44
  • @jillian this question is answered in the javadoc of Serializable: https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html – JB Nizet Feb 17 '16 at 14:54