简体   繁体   中英

Usnig session as instance variable

Many methods of my Servlet use HTTPSession. It is thread-safety to declare HTTPSession variable as instance variable?

By defaut Servlets are not thread safe . And moreover, a servlets instance will invoked for many clients. It is absolutely wrong to have session as instance variable.

Reference:

Is a Servlet thread-safe

Write thread safe servlets

No, it is not safe. a servlet is created when the application starts. The Servlet has only one instance (which means multiple requests/clients use the same servlet), which is why you should avoid having any instance variables.

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