How to conditionally display elements of JSP page depending on user role

前端 未结 2 1371
情话喂你
情话喂你 2020-12-18 16:22

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

2条回答
  •  甜味超标
    2020-12-18 16:29

    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:

    
        

    This will be displayed only if the user has the role "admin".

    This will be displayed only if the user has the role "guest".

    See also:

    • How to disable GET requests to JSP page?
    • Restrict JSP/Servlet access to specific users only

提交回复
热议问题