I'm looking for the function to verify if the user logged belongs to a role. is maybe the following?
pageContext.request.userPrincipal.roles
How should I use it properly along with JSTL to test if the user belong to ADMIN group?
It is not direct call of JavaScript function from JSP, you just prepare the call and call is performed when page is parsed/loaded on the client side:
Steps to Validate a User: 1 We click the link on index.html page to deploy the application. 2 We are then presented with a form, where we enter username and password and click submit. 3 The JSP gets automatically called and it returns the data entered in the form and the result of Validation. More ...
The JSP page is called via a JSP page, HTML page, or JAVA servlet. Once it is called then the cycle mentioned below is triggered to generate dynamic content. This dynamic content is then fed on the web page. Any server can be used in the back end like wildfly, tomcat, or others for this technology to work.
The JSP code is written under separate tags to determine the JSP code but this should all be enclosed under HTML tags like this: <head> All header related things to be included here. The title is also attached to the web page in this section itself. </head> How does JSP Page work in JSP?
You can use Method expression 'request.isUserInRole' in JSP to check whether current authenticated user has a role.
Test this:
<c:if test="${not empty pageContext.request.userPrincipal}">
<c:if test="${pageContext.request.isUserInRole('ADMIN')}">
User ${pageContext.request.userPrincipal.name} in ADMIN Group
</c:if>
</c:if>
Note that: Calling method with/without parameters in EL expression is only supported from JavaEE6 (JSP 2.2 & EL 2.2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With