2

I did the following

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

In my LoginServlet and the following

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

In the other servlet. But currentUser = null, Only when I changed APPLICATION_SCOPE to SESSION_SCOPE it started to work.

So, the question is, why application scope doesn't see the variable I set in session scope, because in my opinion when I create variable in session scope, it is automatically becomes visible in application scope?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233

1 Answers1

0

You are searching attribute in a specified scope only, See java doc:

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

To find in all scopes, use instead 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.

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Yes, I know that, but I just want to know whether the variable set in session scope will be automatically set and visible is application scope, because application scope is "bigger" scope than session one –  May 26 '20 at 11:02
  • @Donny Use instead `findAttribute("authUser")` – Ori Marko May 26 '20 at 11:11
  • So, when variable is set in session scope it is NOT set automatically in application scope? –  May 26 '20 at 11:19
  • @Donny When set in session scope it is NOT set in application scope – Ori Marko May 26 '20 at 11:23