Java servlet 上的全局变量。对所有会话是全局的,还是仅对当前会话是全局的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24469257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:23:17  来源:igfitidea点击:

Global variable on servlet. is global for all sessions, or only for the current session?

javasessionservlets

提问by user3550529

I need to share information while the applicaiton is running; if I have:

我需要在应用程序运行时共享信息;如果我有:

public class example extends HttpServlet
{
    Object globalObject;

    doGet...
    doPost....
}

A user is using the aplication through server and the object globalObject; if another user use the application, will share the object with the first user?

用户通过服务器和对象 globalObject 使用应用程序;如果另一个用户使用该应用程序,是否会与第一个用户共享该对象?

回答by Kevin Bowersox

The same instance of the variable will be used by all requests handled by the servlet. Servlets are not thread safe since only one instance of the servlet is created.

servlet 处理的所有请求都将使用该变量的相同实例。Servlet 不是线程安全的,因为只创建了一个 servlet 实例。

This will cause two users to use the same instance of globalObject.

这将导致两个用户使用相同的globalObject.

回答by Marcelo Bezerra

with HttpSession your variable will be related to each users session, not the application itself

使用 HttpSession 您的变量将与每个用户会话相关,而不是应用程序本身

you can do this as follow

你可以这样做

ServletContext application = getServletConfig().getServletContext();  

String data = "test";  
application.setAttribute("variable", data);  

String data_rtrvd= (String) application.getAttribute("variable"); 

Is JSP code you can do:

您可以执行 JSP 代码吗:

<jsp:useBean id="obj" class="my.package.name.MyClass" scope="application" />

回答by Aniket Thakur

A user is using the aplication through server and the object globalObject; if another user use the application, will share the object with the first user?

用户通过服务器和对象 globalObject 使用应用程序;如果另一个用户使用该应用程序,是否会与第一个用户共享该对象?

Yes!Different threads might be used to render request for different users but the same servlet instance is used.So yes the variable will be common to all requests. In-fact this is why it is said we should not have global variables to ensure thread safety.

是的!不同的线程可能用于为不同的用户呈现请求,但使用相同的 servlet 实例。所以是的,该变量对所有请求都是通用的。事实上,这就是为什么说我们不应该有全局变量的原因to ensure thread safety

回答by Isaac

That depends on how your application server allocates servlets.

这取决于您的应用程序服务器如何分配 servlet。

If your application server allocates only one servlet instance, then yes, all requests will share access to the global variable and you must account for that in your design (unless you choose to implement the deprecated SingleThreadModelinterface, which will guarantee that, while all requests will have access to the global variable, they won't access it concurrently. Don't do it. Find another way).

如果您的应用程序服务器只分配一个 servlet 实例,那么是的,所有请求都将共享对全局变量的访问,您必须在设计中考虑到这一点(除非您选择实现已弃用的SingleThreadModel接口,这将保证,而所有请求都将可以访问全局变量,他们不会同时访问它。不要这样做。寻找另一种方式)。

If your application server allocates more than one servlet instance, then the answer is "not necessarily".

如果您的应用服务器分配了多个 servlet 实例,那么答案是“不一定”。

Obviously, you are often masked from the server's decisions (as to whether to instantiate multiple instances or not), so you must design for safety.

显然,您经常被服务器的决定所掩盖(关于是否实例化多个实例),因此您必须进行安全设计。

回答by Andres

Generally speaking, threads are singletons, so the answer to your question is yes. However, if you want to share data between different users, you should use a true Singleton implementation. Also consider concurrency, because you'll surely have many threads executing at the same time (One for each request that is received by the server)

一般来说,线程是单例的,所以你的问题的答案是肯定的。但是,如果要在不同用户之间共享数据,则应使用真正的单例实现。还要考虑并发性,因为您肯定会同时执行多个线程(服务器收到的每个请求一个)