Search code examples
javajspeljsp-tags

Session attribute not responding inside EL expression


I know this may look like a duplicate question. Unfortunately there is no acceptable, working answer. Even the OP was facing a different issue, not what the question it says.

POJO class below :

private boolean admin = false;
private boolean isNormal = false; 

public void setAdmin(boolean admin) {
    this.admin = admin;
}
public boolean getAdmin() {
    return admin;
}
public void setIsNormal(boolean isNormal) {
    this.isNormal= isNormal;
}
public boolean getIsNormal() {
    return isNormal;
}

// In this class I have many boolean flags like above two. I need to access those in the my JSP

Servlet code below :

System.out.println(responseHeader.getAdmin()); //printed 'True'
session.setAttribute("header", responseHeader);
request.getRequestDispatcher("/DashBoard/Shipper").forward(request, response);

JSP below code :

<%StaticHeader sh = (StaticHeader)session.getAttribute("header");//getting the StaticHeader Object from the session
pageContext.setAttribute("headerFromSession",sh); // set the StaticHeader Object again into PageContext (may be unnecessary): 
%>

None of these below scenarios didn't work and I didn't get any Exceptions either.

1.) <c:if test="${headerFromSession.getAdmin()}"> //seems to be standard, formal way. But it didn't work
2.) <c:if test="${headerFromSession.Admin}"> // Is this legal? I mean, 'admin' is a private variable. 
3.) <c:if test="${headerFromSession.ADMIN}">
4.) <c:if test="${headerFromSession[Admin]}">
5.) <c:if test="${headerFromSession[ADMIN]}">
6.) <c:if test="${headerFromSession}"> //This seems like totally not correct. Because I have many boolean flages which I have already set to the StaticHeader Object

Solution

  • There are a couple of problems here.

    1. Your bean (or "POJO" as you want to call it) doesn't adhere the Javabeans specification as to boolean properties. Particularly that boolean getters have the form of isPropertyName() and not getPropertyName().

      Fix it accordingly:

      private boolean admin = false;
      private boolean normal = false; 
      
      public void setAdmin(boolean admin) {
          this.admin = admin;
      }
      
      public boolean isAdmin() {
          return admin;
      }
      
      public void setNormal(boolean normal) {
          this.normal = normal;
      }
      
      public boolean isNormal() {
          return normal;
      }
      

    2. You're setting a session attribute on the name of a reserved EL variable referring the HTTP request header. Simply put: the ${header} variable is already reserved. This is supposed to be used to access a HTTP request header in EL such as ${header['User-Agent']}.

      Give it a different name:

      session.setAttribute("staticHeader", staticHeader);
      

      Note that I also renamed the Java variable for clarity, because this definitely doesn't represent a "response header". It would otherwise only be confusing to other people reading this code and possibly also to yourself later once you get more fluent in Java Servlets and HTTP (I only still wonder what exactly a "static header" means in this context, as an educated guess I think that you're overcomplicating "user roles" or "user groups", but alas).

      This way it's available in EL as ${staticHeader} and you don't need to mess with that ugly scriptlet workaround of putting a copy in the page context under a different name.


    3. Your attempts to access the bean property doesn't adhere the EL specification. You should be using the form of ${bean.propertyName} and not ${bean.PropertyName} and for sure not the other forms. If you really need the brace notation (because it's to be supplied as another variable), then you still need to make sure that the string value is propertyName as in ${bean['propertyName']}.

      So, this should do:

      <c:if test="${staticHeader.admin}">
      

      Do note that this doesn't access the private field, instead it invokes the isAdmin() getter method. Moreover, the presence of the private field is irrelevant to EL, you could even remove it altogether.


    I warmly recommend you (and all commenters on the question) to take a pause on the JSP/Servlet project you're currently working and go through a sane JSP/Servlet book/tutorial. All of above is covered therein.