如果我在会话范围内保存变量,它在应用程序范围内是否可见?

If I save variable in session scope, is it visible in application scope?

我做了以下

getJspContext().setAttribute("authUser", user, PageContext.SESSION_SCOPE);` 

在我的LoginServlet和下面

User currentUser = (User) getJspContext().getAttribute("authUser", PageContext.APPLICATION_SCOPE);

在另一个servlet中。但是 currentUser = null,只有当我将 APPLICATION_SCOPE 更改为 SESSION_SCOPE 时,它才开始工作。

所以,问题是,为什么应用程序作用域看不到我在会话作用域中设置的变量,因为在我看来,当我在会话作用域中创建变量时,它是在应用程序范围内自动可见?

您正在 指定范围内搜索属性 ,参见 java doc:

Return the object associated with the name in the specified scope or null if not found.

要在所有范围内查找,请改用 findAttribute("authUser")

Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.