3

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
Community
  • 1
  • 1
Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
  • Could you please show your servlet code? That would help better to understand – Parth Soni May 02 '15 at 06:06
  • Okay, I assume `header` is a type of `StaticHeader`, so how you're setting property of `SHIPPER` of `header`? – Parth Soni May 02 '15 at 06:14
  • If `header` is already properly set to a broader scope, then why do you need to set it to a narrower scope again? Besides, go with JSTL excluding Scriptlets in its entirely. – Tiny May 02 '15 at 06:17
  • yes, why not only use `` instead of ``, as `header` also contains boolean value. – Parth Soni May 02 '15 at 06:19
  • @Tiny I thought session variables are not directly accessible inside EL. So i took it inside PageContext. Might be additional work, but for the problem what i'm facing will not have any relation to that. isn't it? – Jude Niroshan May 02 '15 at 06:20
  • {header} itself is not a boolean.It's the StaticHeader Object what i received from the session. It contains about 10 different boolean flags. So how would i specifically this the 'isAdmin' variable value out? – Jude Niroshan May 02 '15 at 06:23
  • They are accessible in EL such as `${sessionScope.name}` or using brace notations `${sessionScope[name]}`. – Tiny May 02 '15 at 06:28
  • What happens when you try the solution that @Parth suggested - ``? – Kartic May 02 '15 at 09:22
  • Both approaches of Parth & Tiny should work! have you tried any? – Nomesh DeSilva May 02 '15 at 09:26

1 Answers1

3

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555