File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Hide/show table based on user role Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Hide/show table based on user role" Watch "Hide/show table based on user role" New topic
Author

Hide/show table based on user role

johnny clarke
Greenhorn

Joined: Feb 02, 2013
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60049
    
  65

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.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60049
        
      65

    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

    Joined: Feb 02, 2013
    Posts: 7
    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

    Joined: Jan 10, 2002
    Posts: 60049
        
      65

    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

    Joined: Feb 02, 2013
    Posts: 7
    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

    Joined: Feb 02, 2013
    Posts: 7
    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

    Joined: Jan 10, 2002
    Posts: 60049
        
      65

    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

    Joined: Jan 10, 2002
    Posts: 60049
        
      65

    P.S. Pay special attention to line 35.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Hide/show table based on user role
     
    Similar Threads
    Error comes whilke compiling the jsp page
    problem in running a servlet
    ResultSet is closed !
    jsp:include and c:import combination/nesting
    HTML does not print properly - table width exceeding 100%?