• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hide/show table based on user role

 
johnny clarke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im quite new to jsp and java and im having a problem with hiding a table based on a users role. So if the user is admin return the resultset and if not hide the resultset. My error is commented in the code below. Any help is appreciated.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using the JSTL conditional tags, not Java code in your JSP. Using Java scriptlets in a JSP is an outdated and obsolete technique from over 11 years ago. (And the very type of problem you are having is one of the 17 bazillion reasons that Java code in a JSP has been discredited for over a decade.)

Here are some tips for proceeding:

First of all, rather than using a magic number of -1 to mean anything, use a Boolean value. Magic numbers are just poor practice all around.

Assuming that there is a Boolean property named admin in the session, the condition check simply becomes:

Result sets should never be sent to a JSP. That's another really poor practice that should be avoided. The information from the result set should be copied into normal Java objects and collections before being sent to the JSP.

Hope that helps get you started on a better path.

I often recommend that novices to JSP read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  • and read this JspFaq entry.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another point: what's up with this line?
    Firstly, that's not something you really ever need to be doing in a JSP, so I suspect you may be trying to do something else, but I'm not sure what.

    And, be sure to follow Java naming conventions in all code. Variable names should always start with a lowercase letter.
     
    johnny clarke
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:Another point: what's up with this line?
    Firstly, that's not something you really ever need to be doing in a JSP, so I suspect you may be trying to do something else, but I'm not sure what.

    And, be sure to follow Java naming conventions in all code. Variable names should always start with a lowercase letter.

    Cheers. Unfortunately I have to write my JSPs this way. I will read through the material and hopefully get it working. Thanks again.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    johnny clarke wrote:Unfortunately I have to write my JSPs this way.

    Why?

    In any case, check your syntax. It's painful to mix Java with JSP, so it's easy to forget when to include all that gnarly <% markup.
     
    johnny clarke
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:Another point: what's up with this line?
    Firstly, that's not something you really ever need to be doing in a JSP, so I suspect you may be trying to do something else, but I'm not sure what.

    And, be sure to follow Java naming conventions in all code. Variable names should always start with a lowercase letter.

    This holds the path to my databse with password and name.
     
    johnny clarke
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:
    johnny clarke wrote:Unfortunately I have to write my JSPs this way.

    Why?

    In any case, check your syntax. It's painful to mix Java with JSP, so it's easy to forget when to include all that gnarly <% markup.

    Im intern in company and this is how they do it. Maybe when I take over i can change that.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Understood. Just be aware that they're using the equivalent of vacuum tubes. It would behoove you to learn more modern ways of working for your next steps.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    P.S. Pay special attention to line 35.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic