Servlet context scope vs global variable

前端 未结 2 1767
谎友^
谎友^ 2020-12-24 13:24

What (if any) is the difference between storing a variable in the ServletContext and just having it as a public static member of one of the classes?

Instead of writi

2条回答
  •  余生分开走
    2020-12-24 14:17

    The web container knows about your servlet context, but not about your static variables which as skaffman says are private to your classloader.

    Anything that cause two different requests to be served by an application instance in a different classloader (this could be server restarting, web application redeployment, or multi-node servers) will case your logic to break. The servlet context will survive these things as the web container knows about it and can serialize it or have a common repository.

提交回复
热议问题