servlet 上的全局变量.对所有会话是全局的,还是仅对当前会话是全局的? [英] Global variable on servlet. is global for all sessions, or only for the current session?

查看:53
本文介绍了servlet 上的全局变量.对所有会话是全局的,还是仅对当前会话是全局的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

public class example extends HttpServlet
{
    Object globalObject;

    doGet...
    doPost....
}

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

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 使用应用程序;如果另一个用户使用该应用程序,是否会与第一个用户共享该对象?

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?

是的! 可能会使用不同的线程来为不同的用户呈现请求,但使用相同的 servlet 实例.所以是的,该变量对所有请求都是通用的.事实上,这就是为什么说我们不应该使用全局变量以确保线程安全.

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 上的全局变量.对所有会话是全局的,还是仅对当前会话是全局的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆