开发者

Please clarify a few points concerning Java Servlets

开发者 https://www.devze.com 2022-12-30 01:30 出处:网络
suppose, I use Tomcat as a web container. Is it true that once all the servlet开发者_Python百科s found in a web-app/WEBAPPNAME

suppose, I use Tomcat as a web container.

Is it true that once all the servlet开发者_Python百科s found in a web-app/WEBAPPNAME

are init(IALIZED) then every change of a Servlet's property will be seen to every session.

So session 1 changes a property userName of a Servlet1 from "user1" to "user2"

session 1 is closed.

session 2 starts. It will see "user2" as the only value of Servlet1.userName property??

Any change of a Servlet's field will be seen to all subsequent sessions?

Are servlets singletons, aren't they?


Any change of a Servlet's field will be seen to all subsequent sessions?

Practically yes. That is why it is strongly not recommended to store data directly within servlets, as servlets are not thread safe. Instead, data should be stored in the servlet context, the session or the request context.

Are servlets singletons, aren't they?

Sort of yes, in the sense that there is one instance of each configured servlet within the same servlet container.


Yes, as long as you aren't using the SingleThreadModel, in which case there can be multiple instances of a Servlet. See e. g. http://docstore.mik.ua/orelly/java-ent/servlet/ch03_04.htm

In any case, I wouldn't rely on this. It's much better to write servlets in a way, that they don't depend on that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号