2

How to load menu on webpage depends upon login user? I want to make websites where some menu will show before login and after login it will show more menu depends upon login user if admin is login then some administraive menu will appear if normal user is login then some different menu will be added. I want to build this project using JSP/Servlet. When user click on any menu total page will not be reloaded only some part will be changed where show the details description of this menu.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

3

You can just use JSTL to programmatically control the flow in the HTML output of the JSP. You can check the role of the currently logged-in user by HttpServletRequest#isUserInRole() which returns a boolean.

As you're using Servlet 3.0, you'll also be able to take benefit of the new EL 2.2 support of invoking methods with arguments. So, this should do:

<c:if test="${pageContext.request.isUserInRole('admin')}">
    <p>This will be displayed only if the user has the role "admin".</p>
</c:if>
<c:if test="${pageContext.request.isUserInRole('guest')}">
    <p>This will be displayed only if the user has the role "guest".</p>
</c:if>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You could have different menus in different jsps and then include those jsps based upon the logged in user.

For instance...

<%if(userRole.equals("admin")){%>
   <jsp:include page="../menu/admin_menu.jsp" />
<%}%>
<%if(userRole.equals("user")){%>
   <jsp:include page="../menu/user_menu.jsp" />
<%}%>
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • Note that this is quite an old school way of writing JSPs and officially discourgaged since JSP 2.0 which was released almost a decade ago. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202 – BalusC Feb 16 '12 at 20:05
  • @BalusC indeed it is but if he can't use JSTL, this will work for him. Your post on using JSTL was excellent and I've looked at it many times and attempted to convince my boss to go this way but haven't been successful yet so I'm stuck with good ole scriptlets. – Zack Macomber Feb 16 '12 at 20:10
  • @Zack Macomber using this procedure login when user is verified and load the proper page all menus are loaded properly.now I want that when user/admin click on menus the total pages will not reloaded only certain part will show the detail,can u help me?and I want that when user scroll down the page data will show accrodingly I donot want that all data will be loaded totally.Please help me. –  Feb 18 '12 at 15:17
  • @moin - the only way I know to make the total page not reload is to use AJAX - have you ever heard of AJAX? Also, for the scrolling down the page question, you would use Javascript and possibly AJAX for that too. I would suggest that you open up other StackOverflow questions for those issues if you need further assistance. – Zack Macomber Feb 18 '12 at 18:43