Marko
Marko

Reputation: 522

Why does keyword "transient" has no effect for Servlet HTTPSession in Java?

What does the transient Keyword exactly mean? I have a Class attribute marked as transient:

public class NodeClassifier {

   private transient HashMap<String, Node> nodeCache = new HashMap<>();

...
}

After storing and recovering an NodeClassifier Object into the HttpSession the Attribute still has the Value from previous Session.

Shouldn't it be empty?

The Environment is a plain Servlet running on Glassfish4.

Upvotes: 1

Views: 986

Answers (1)

Thilo
Thilo

Reputation: 262824

transient means that the value will not be serialized (using default Java object serialization), when the object is written out as bytes.

Serialization of a session may or may not happen (it is only needed to pass the session between processes, or to persist it to disk, but usually not needed in a single JVM servlet container that can just keep them in memory), so you should not rely on values being "lost" that way.

If you don't want stuff to survive in the session, don't put it there. Consider using request attributes instead.

Upvotes: 5

Related Questions