Servlet context scope vs global variable

前端 未结 2 1758
谎友^
谎友^ 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:16

    Well they're not quite the same; servlet-context-scope is private to the webapp, whereas static scope is private to the classloader. Depending on your container and how it's configured, these may or may not be the same. When thinking in terms of webapps and JavaEE, using a context-coped field is going to be more reliably portable. Also, context-scoped attributes are easier to access from JSPs, i.e. you don't need scriptlets to get to them.

提交回复
热议问题