我正在尝试阻止访客用户访问管理页面。
这是我捕获角色1 for admin
和0 for guest
。
<%
HttpSession session = request.getSession();
String sessionVal= (String)session .getAttribute("loggedIn");
int role = (Integer)session .getAttribute("role");
System.out.println(" role "+ role);
if(sessionVal == null)
{
response.sendRedirect("/project/pages/login.jsp");
}
else
{
%>
// All html code goes here
这是html
导航代码
<ul id="nav">
<li><a href="/project/pages/home.jsp">Dashboard</a></li>
<li ><a href="/project/pages/profile.jsp">Profile</a></li>
<li><a href="/project/pages/setting.jsp">Setting</a></li> <li><a href="/project/pages/admin.jsp">Admin</a></li>
</ul>
现在如何阻止访客用户(role == 0
)访问管理页面。或if role == 0
如何隐藏admin.jsp
。
答案 0 :(得分:3)
为什么不
if(sessionVal == null || role == 0)
隐藏JSP中的链接
<%
HttpSession session = request.getSession();
int role = (Integer)session .getAttribute("role");
System.out.println(" role "+ role);
if(role != 0)
{
%>
<a href="admin.jsp">Top Secret Link</a>
<%
}
答案 1 :(得分:3)
<%
String sessionVal= (String)session .getAttribute("loggedIn");
int role = (Integer)session .getAttribute("role");
if (sessioanVal == null || role == 0) {
%>
<li><a href="/project/pages/home.jsp">Dashboard</a></li> (simplle which you want to hide)
<%
} else {
%>
<li ><a href="/project/pages/profile.jsp">Profile</a></li>
(which you want to show)
<%
}
%>
答案 2 :(得分:1)
以下几种方式之一......
在admin.jsp页面的开头,你可以这样:
HttpSession monitorSession = request.getSession();
String sessionVal= (String)monitorSession.getAttribute("loggedIn");
int role = (Integer)monitorSession.getAttribute("role");
if(role ==0){ //make the check here
response.sendRedirect("somePage.jsp");
}
也可以用同样的方式隐藏导航中的admin.jsp选项