You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text/web/index.jsp

42 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 页面指令:
contentType="text/html;charset=UTF-8"指定了该JSP页面返回给客户端的内容类型是HTML格式并且字符编码采用UTF-8这样可以确保页面能够正确显示包含各种语言文字尤其是中文等非ASCII字符的内容避免出现乱码情况。
language="java"表明该JSP页面使用Java语言来编写脚本代码JSP本质上最终会被服务器编译成ServletJava类来执行这里明确了其脚本所基于的编程语言。
-->
<html>
<head>
<title>TITLE</title>
</head>
<body>
<%
// 以下这段代码基于服务器端的会话session对象来判断用户的角色并根据不同的角色进行页面转发操作。
// 它通过检查session对象中存储的特定属性student、teacher、admin是否存在来确定用户身份然后将请求转发到相应的初始页面
// 如果这些属性都不存在意味着用户未登录或者登录状态失效等情况就将请求转发到login.jsp页面让用户进行登录操作。
if (session.getAttribute("student")!= null) {
// 使用session.getAttribute("student")尝试从当前会话中获取名为student的属性值
// 如果获取到的值不为null表示当前用户的角色是学生此时执行下面的转发操作
// 利用JSP的转发标签<jsp:forward>将请求转发到WEB-INF/student/sIndex.jsp页面。
// WEB-INF目录下的资源通常是受保护的不能直接通过浏览器URL访问需要通过服务器端的转发等内部机制来访问这样可以提高页面的安全性避免用户直接绕过权限检查去访问一些应该有权限限制的页面。
%>
<jsp:forward page="/WEB-INF/student/sIndex.jsp" />
<%
} else if (session.getAttribute("teacher")!= null) {
// 类似地如果student属性不存在但是teacher属性存在于会话中说明用户角色是教师就会把请求转发到WEB-INF/teacher/tIndex.jsp页面。
%>
<jsp:forward page="/WEB-INF/teacher/tIndex.jsp" />
<%
} else if (session.getAttribute("admin")!= null) {
// 若student和teacher属性都不存在而admin属性存在表明用户是管理员角色会将请求转发到WEB-INF/admin/aIndex.jsp页面。
%>
<jsp:forward page="/WEB-INF/admin/aIndex.jsp" />
<%
} else {
// 前面几个条件都不满足也就是上述代表不同角色的属性在会话中都不存在时会将请求转发到login.jsp页面引导用户进行登录
// 建立相应的会话属性来标识其角色,后续再次访问时就能根据角色转发到对应的页面了。
%>
<jsp:forward page="login.jsp" />
<%
}
%>
</body>
</html>