Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

HTTPSession Attribute

  • 16-03-2007 10:58AM
    #1
    Registered Users, Registered Users 2 Posts: 7,541 ✭✭✭


    Hey all,

    Doing some work on java servlets at the mo. As part of the application I set an attribute for the HTTPSession to determine the user type.

    i.e.
    [PHP]this.getSession.setAttribute("isSuperUser","false");[/PHP]

    At various points in the application this attribute is used in if statements:

    [PHP]if(this.getSession().getAttribute("isSuperUser").toString().equalsIgnoreCase("true")){[/PHP]

    Now I find the above code a bit ugly/childish. But since I can't cast an Object to a boolean it's the only way I can do it at the moment. Anyone got an idea on how to do it better? Apart from an isSuperUser method?

    [PHP]boolean isSuperUser(){
    if(this.getSession().getAttribute("isSuperUser").toString().equalsIgnoreCase("true"))
    return true
    else
    return false
    }
    [/PHP]

    Thanks
    R


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    How about only setting the isSuperUser attribute if they are a superuser, and then use the absense of it to say they arent one...

    when they log in

    [PHP]
    if(superUser)
    request.getSession().setAttribute("superUser","true");
    [/PHP]

    Put this code in a helper class somewhere

    [PHP]
    public static boolean isSuperUser(HttpServletRequest request)
    {
    boolean super=false;

    if(request.getSession().getAttribute("superUser")!=null)
    super=true;

    return super;
    }
    [/PHP]

    There's also nothing stopping you putting a boolean into the session like in your original example

    [PHP]
    if(superUser)
    request.getSession().setAttribute("superUser",Boolean.TRUE);
    else
    request.getSession().setAttribute("superUser",Boolean.FALSE);


    if( ((Boolean)request.getSession().getAttribute("superUser")).booleanValue()==true)
    [/PHP]


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Don't know a whole lot about JSP in particular.

    It does seem a little messy. As a push, and at a slight efficiency cost, you could pass a java.lang.Boolean object to the request. This is java's way of essentially allowing you to use primitives where objects should be used.

    So
    isSuperUser = new Boolean(false);
    this.getSession.setAttribute("isSuperUser", isSuperUser)
    
    .....
    //When getting the value of this attribute
    if(this.getSession().getAttribute("isSuperUser").booleanValue()){ 
    //And so on
    

    My Java's a little rusty, so I can't guarantee this will work.


  • Registered Users, Registered Users 2 Posts: 7,541 ✭✭✭irlrobins


    Cheers guys. I had forgotten about using Boolean as opposed to boolean. That will make it a bit neater.

    Thanks


Advertisement