java - attribute with same name but different scopes can be overridden or maintained? -


in jsp happened if 2 attributes added same name different scopes. when try code got page scope attribute value null , session scope value stored.

   <%  pagecontext.setattribute("kumar", "mca", pagecontext.page_scope);       pagecontext.setattribute("kumar", "bsc", pagecontext.session_scope);     %> 

i tested code , found expected results.

<%  pagecontext.setattribute("kumar", "mca", pagecontext.page_scope); pagecontext.setattribute("kumar", "bsc", pagecontext.session_scope);  pagecontext.setattribute("kumar", "inter", pagecontext.application_scope);  %>  attribute in page scope:       <%=pagecontext.getattribute("kumar", pagecontext.page_scope)%>  attribute in session scope:    <%=pagecontext.getattribute("kumar", pagecontext.session_scope)%>  again attribute in application scope: <%=pagecontext.getattribute("kumar",pagecontext.application_scope)%> 

output

attribute in page scope: mca  attribute in session scope: bsc  again attribute in application scope: inter  

every scope different. specified in comments already,there no such thing override in high level scope , low level scope.for better understanding,see concrete implementation of getattribute method in org.apache.jasper.runtime.pagecontextimpl

 private object dogetattribute(string name, int scope) {          switch (scope) {          case page_scope:              return attributes.get(name);           case request_scope:              return request.getattribute(name);           case session_scope:              if (session == null) {                  throw new illegalstateexception(localizer                          .getmessage("jsp.error.page.nosession"));              }              return session.getattribute(name);            case application_scope:              return context.getattribute(name);           default:              throw new illegalargumentexception("invalid scope");          }      } 

see also


Comments