简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM