Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

HTTPSession Attribute

  • 16-03-2007 9: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 Posts: 2,013 ✭✭✭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,317 ✭✭✭✭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