Lock jsp page using javascript

how to block a jsp page (I want when I click the links to redirect each page, I want to block some specific pages for specific users) I create a java script function to extract jsp pages for each user (pages that the user can access). But I'm not going to block other pages for the same user.

+6
source share
4 answers

use js document.getElementById("id name of link").style.display = 'none'; to remove the link from the page and use "block" instead of "none" to display the link.

+4
source

You can use event.preventDefault(); and have a variable saying that the user should or should not be blocked. Check out the following example:

 var BlockUser = true; function CheckUser() { if ( BlockUser ) { event.preventDefault(); } } 
 <a href="http://stackoverflow.com/">Link for any user</a> <br> <a href="http://stackoverflow.com/" onclick="CheckUser()">Link for certain users</a> 
+4
source

Pure jsp solution:

provided that you have an array of available links: List<String> links , which you pass under the same name for the request (or you can get it from the user, does not matter, suppose you have an array of these links, despite that you get this), then you can do something like:

  ... <c:forEach var="link" items="${links}"> <a href="${link}" <c:if test="/*here you test if user have access, i dont know how you do it*/"> class="inactiveLink" </c:if>>page link</a> </c:forEach> ... 

Where ... is the rest of your jsp and define a style

 .inactiveLink { pointer-events: none; cursor: default; } 

Note that to use foreach, you must define the jstl taglib at the top of jsp:

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

If you do not know what jstl is and what EL is usually

It was said about disabling css and js, if you want them to be completely inaccessible, you can just print only allowed links:

 ... <c:forEach var="link" items="${links}"> <c:if test="/*here you test if user have access, i dont know how you do it*/"> <a href="${link}">page link</a> </c:if> </c:forEach> ... 
+2
source

You can use this

 document.getElementById( "id of your link element" ).style.display = 'none'; 

style.display used to not display anything by setting it to none

+1
source

Source: https://habr.com/ru/post/1015126/


All Articles