0

I am trying to call a java method in jsp. The main idea is to hide menu based on the user who logs in. Please go through the code and provide me a solution guys.

The java class flows like this,

public class UserVerification {

public static void main(String[] args) {
UserVerification obj = new UserVerification();
System.out.print(obj.GetUserVerification("abc"));
}

public int GetUserVerification(String empID) {
int roleId = 0;
try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");

Connection connection = DriverManager.getConnection("");

PreparedStatement ps = connection.prepareStatement("");
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
roleId = Integer.parseInt(resultSet.getString(""));
int RoleID = 0;
UserMaster um = new UserMaster();
um.getRoleID();

while ((RoleID == roleId) && resultSet.next()) {
UserInfoDisplayController ui = new UserInfoDisplayController();
List<UserMaster> objUser = ui.GetUserInfoDisplayController(1);
System.out.print(objUser.size());
for (UserMaster um1 : objUser) {
um1.getEmpID();
um1.getUserName();
um1.getEmailID();
um1.getRoleID();
um1.getSupervisor();
um1.getTeamID();
}
}
}
ps.close();
ps.close();
connection.close();

} catch (Exception e) {
e.printStackTrace();
}
return roleId;
}
}

And the javascript and jsp in which i call this are shown below,

<script type="text/javascript">
function Validation(RoleID) {
alert("one");
var el = document.getElementById(cssmenu);
alert("two");
if (RoleID != 2) {
el.style.visibility == "visible";
} else {
el.style.visibility == "hidden";
}
alert("three");
}
alert("four");
</script>



<div id='cssmenu'>
<ul>
<% 
if (RoleID != 2) {
%>
<li id="menu"><a href="UserMaster.jsp" >User Master</a></li>
<li id="menu"><a href="SkillMaster.jsp" >Skill Master</a></li>
<li id="menu"><a href="SkillMapping.jsp" >Skill Mapping</a></li>
<li id="menu"><a href="Report.jsp" >Reports</a></li>
<%
}`enter code here`
%>
<li><a href="MySkills.jsp" >My Skills</a></li>
</ul>
</div>
  • 1
    better take a look [how to avoid java code in jsp files](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq=1) then, rethink your design. – Abhishek Nayak Apr 28 '14 at 11:30
  • 1
    Its not good to have java code in jsps. THese links may help you. http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/ and http://www.thejavageek.com/2013/12/30/advantages-jsp-servlets/ – Prasad Kharkar Apr 28 '14 at 11:33

2 Answers2

1
put this code first in jsp.

<%
UserVerification  uv = new UserVerification ();
int roleID = uv.GetUserVerification("empID");
%>

now check roleID with if condition anywhere in jsp.
Sunny
  • 308
  • 2
  • 14
0

You should use like this code

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<sec:authorize access="isAuthenticated()">
    // Only authenticated users
</sec:authorize>
Mufanu
  • 534
  • 7
  • 18