Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hide/display links with jsp

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,
Does anyone have any jsp examples of how to display or hide html links based upon a passed in variable, e.g. a user group? When a user logs into my website, they are placed into a group of either limited or super users. My web page has several links on it, and if a limited user clicks on a protected super user link, they will receive a not authorized to view page. I would like to create one jsp page that has the ability to hide and display the links based upon the user group. Do you have or know where I could find some examples of this?

thanks for any help!
 
You could just group the super links together and and say...

If QueryColumnName = superuserstatus then display the super user links...

 
I did this like that (consider that the boolean isSuperUser is set to true when the user has all the rights :
Code:
<%
if (isSuperUser) {
  out.print(&quot;<A href='admin.jsp'>&quot;);
  out.print(&quot;Admin&quot;);
  out.println(&quot;</A>&quot;);
} else {
  out.println(&quot;&quot;);
}
%>

That works well.
Water is not bad as soon as it stays out human body ;-)
 
But that defeats the concept of trying to keep Java code out of the presetnation layer (JSP)... another solution, more elegant, and only slightly more time consuming, is to create new tags and use those.

For instance...

public class IsSuperUserTag extends BodyTagSupport {

public int doStartTag() throws JspException {
boolean OK = false;
User user = (User) pageContext.getSession().getAttribute(&quot;USER&quot;);
if (user != null) {
OK = user.IsSuperUser();
}
if (OK) {
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
}
}

Then you create a TLD for your app, add this class, and you can then do JSP code like this:

...
<app:IsSuperUser>
<a href=&quot;whatever&quot;>top secret link here</a>
</app:IsSuperUser>
...

That way, the people maintaining the JSP don't have to know anything about Java code.

Does that help or is it a pain in the neck?

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top