|
|
|
@ -25,169 +25,109 @@ import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
// 使用WebServlet注解将该类映射到"/loginServlet"这个URL路径,表明这是一个用于处理对应网络请求的Servlet类,
|
|
|
|
|
// 从类名可以看出,主要负责处理用户登录相关的业务逻辑。
|
|
|
|
|
@WebServlet("/loginServlet")
|
|
|
|
|
public class LoginServlet extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
// 重写doPost方法,用于处理HTTP POST请求
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
// 初始化用于存储登录成功后的学生对象,初始值设为null,后续根据登录验证结果进行赋值
|
|
|
|
|
Student loginStudent = null;
|
|
|
|
|
// 初始化用于存储登录成功后的教师对象,初始值设为null,后续根据登录验证结果进行赋值
|
|
|
|
|
Teacher loginTeacher = null;
|
|
|
|
|
// 初始化用于存储登录成功后的管理员对象,初始值设为null,后续根据登录验证结果进行赋值
|
|
|
|
|
Admin loginAdmin = null;
|
|
|
|
|
|
|
|
|
|
// 设置请求的字符编码为"utf-8",确保能正确解析包含中文等多字节字符的请求参数
|
|
|
|
|
//设置编码
|
|
|
|
|
request.setCharacterEncoding("utf-8");
|
|
|
|
|
|
|
|
|
|
// 从请求中获取名为"verifycode"的参数值,该参数通常是用户在登录页面输入的验证码,用于后续验证码校验
|
|
|
|
|
//获取数据
|
|
|
|
|
String verifycode = request.getParameter("verifycode");
|
|
|
|
|
// 从请求中获取名为"id"的参数值,该参数一般代表用户输入的登录账号(如学生学号、教师工号、管理员账号等),具体含义根据角色而定
|
|
|
|
|
String loginid = request.getParameter("id");
|
|
|
|
|
// 从请求中获取名为"password"的参数值,该参数是用户输入的登录密码
|
|
|
|
|
String loginpassword = request.getParameter("password");
|
|
|
|
|
|
|
|
|
|
// 验证码校验相关逻辑
|
|
|
|
|
// 获取当前请求对应的HttpSession对象,用于获取会话中存储的验证码信息(之前生成并存储在会话中的验证码)
|
|
|
|
|
//验证码校验
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
// 从会话中获取名为"CHECKCODE_SERVER"的属性值,该值就是服务器端生成并存储的验证码,类型转换为String
|
|
|
|
|
String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
|
|
|
|
|
// 移除会话中的"CHECKCODE_SERVER"属性,确保验证码只能使用一次,防止重复利用
|
|
|
|
|
session.removeAttribute("CHECKCODE_SERVER");
|
|
|
|
|
|
|
|
|
|
// 判断服务器端存储的验证码是否为null(可能出现异常情况未生成验证码等),或者与用户输入的验证码不相等(不区分大小写进行比较),
|
|
|
|
|
// 如果满足此条件,则表示验证码不正确
|
|
|
|
|
if (checkcode_server == null ||!checkcode_server.equalsIgnoreCase(verifycode)) {
|
|
|
|
|
// 在请求属性中设置"login_msg"属性,添加提示信息,说明验证码错误,用于后续在页面上展示给用户
|
|
|
|
|
request.setAttribute("login_msg", "验证码错误");
|
|
|
|
|
// 在请求属性中设置"loginid"属性,将用户输入的登录账号再次存入请求属性,方便在返回登录页面时可以回显给用户
|
|
|
|
|
request.setAttribute("loginid", loginid);
|
|
|
|
|
// 在请求属性中设置"loginpassword"属性,将用户输入的登录密码再次存入请求属性,方便在返回登录页面时可以回显给用户
|
|
|
|
|
request.setAttribute("loginpassword", loginpassword);
|
|
|
|
|
// 将请求转发到"/login.jsp"页面,通常这个页面就是登录页面,返回给用户并展示验证码错误的提示信息以及回显账号密码等内容
|
|
|
|
|
request.getRequestDispatcher("/login.jsp").forward(request, response);
|
|
|
|
|
session.removeAttribute("CHECKCODE_SERVER");//确保验证一次性
|
|
|
|
|
if (checkcode_server == null || !checkcode_server.equalsIgnoreCase(verifycode)) {
|
|
|
|
|
//验证码不正确
|
|
|
|
|
request.setAttribute("login_msg","验证码错误");
|
|
|
|
|
//跳转页面
|
|
|
|
|
request.setAttribute("loginid",loginid);
|
|
|
|
|
request.setAttribute("loginpassword",loginpassword);
|
|
|
|
|
request.getRequestDispatcher("/login.jsp").forward(request,response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 封装对象相关逻辑
|
|
|
|
|
// 再次从请求中获取名为"id"的参数值,此处代码可能存在重复获取的情况(前面已经获取过loginid,可根据实际情况优化),该值用于构建用户对象的标识属性
|
|
|
|
|
//封装对象
|
|
|
|
|
String id = request.getParameter("id");
|
|
|
|
|
// 再次从请求中获取名为"password"的参数值,此处代码可能存在重复获取的情况(前面已经获取过loginpassword,可根据实际情况优化),该值用于构建用户对象的密码属性
|
|
|
|
|
String password = request.getParameter("password");
|
|
|
|
|
// 从请求中获取名为"roles"的参数值,该参数用于区分用户的角色(如"student"表示学生、"teacher"表示教师、"admin"表示管理员),以便后续进行不同角色的登录验证逻辑
|
|
|
|
|
String roles = request.getParameter("roles");
|
|
|
|
|
|
|
|
|
|
// 公告加载相关逻辑
|
|
|
|
|
// 创建NotifyService的实现类实例,用于调用业务逻辑层方法来获取公告相关信息,这里应该是获取所有的公告列表
|
|
|
|
|
NotifyService notifyService = new NotifyServiceImpl();
|
|
|
|
|
// 调用业务逻辑层的find方法,获取公告列表数据,返回的是Notify类型的列表
|
|
|
|
|
//公告加载
|
|
|
|
|
NotifyService notifyService= new NotifyServiceImpl();
|
|
|
|
|
List<Notify> notifys = notifyService.find();
|
|
|
|
|
// 将获取到的公告列表存放到HttpSession中,属性名为"notifys",方便在后续页面中(不同角色登录后的页面)可以获取并展示公告信息
|
|
|
|
|
session.setAttribute("notifys", notifys);
|
|
|
|
|
session.setAttribute("notifys",notifys);
|
|
|
|
|
|
|
|
|
|
// 根据不同角色进行登录验证、对象封装、会话保存以及页面跳转等逻辑处理
|
|
|
|
|
//判断roles封装对象、保存session、调用不同Service查询
|
|
|
|
|
if ("student".equals(roles)) {
|
|
|
|
|
// 创建一个Student对象,用于封装学生登录相关的信息,作为传递给业务逻辑层进行登录验证的数据对象
|
|
|
|
|
|
|
|
|
|
Student student = new Student();
|
|
|
|
|
// 设置学生对象的学号属性,使用前面获取到的id值
|
|
|
|
|
student.setS_id(id);
|
|
|
|
|
// 设置学生对象的密码属性,使用前面获取到的password值
|
|
|
|
|
student.setS_password(password);
|
|
|
|
|
|
|
|
|
|
// 创建StudentService的实现类实例,用于调用业务逻辑层中与学生登录相关的方法
|
|
|
|
|
StudentService service = new StudentServiceImpl();
|
|
|
|
|
// 调用业务逻辑层的login方法,传入封装好的学生对象,进行登录验证,将验证通过后返回的学生对象赋值给loginStudent变量(如果验证失败则为null)
|
|
|
|
|
StudentService service= new StudentServiceImpl();
|
|
|
|
|
loginStudent = service.login(student);
|
|
|
|
|
|
|
|
|
|
// 判断登录验证是否成功,即loginStudent不为null,表示找到了对应的学生记录,登录成功
|
|
|
|
|
if (loginStudent!= null) {
|
|
|
|
|
// 将登录成功的学生对象存放到HttpSession中,属性名为"student",方便后续在其他页面判断用户是否登录以及获取学生相关信息
|
|
|
|
|
if (loginStudent != null) {
|
|
|
|
|
session.setAttribute("student", loginStudent);
|
|
|
|
|
// 在会话中设置"html_title"属性,值为"学生端",可能用于在页面上显示不同的标题栏等内容,标识当前处于学生端页面
|
|
|
|
|
session.setAttribute("html_title", "学生端");
|
|
|
|
|
// 重定向到"studentIndexServlet",通常这个Servlet会将请求转发到学生端对应的首页(如"/WEB-INF/student/sIndex.jsp"等页面),实现登录成功后跳转到学生端首页的功能
|
|
|
|
|
// (此处注释掉了直接转发的代码,选择使用重定向方式,重定向会重新发起一个请求,而转发是在服务器内部直接跳转,两者有不同的应用场景和效果)
|
|
|
|
|
// request.getRequestDispatcher("/WEB-INF/student/sIndex.jsp").forward(request, response);
|
|
|
|
|
// request.getRequestDispatcher("/WEB-INF/student/sIndex.jsp").forward(request,response);
|
|
|
|
|
response.sendRedirect("studentIndexServlet");
|
|
|
|
|
} else {
|
|
|
|
|
// 如果登录验证失败,即loginStudent为null,表示用户名或密码错误等情况
|
|
|
|
|
// 在请求属性中设置"login_msg"属性,添加提示信息,说明用户名或密码错误,用于后续在登录页面展示给用户
|
|
|
|
|
}else {
|
|
|
|
|
//登录失败 提示信息
|
|
|
|
|
request.setAttribute("login_msg", "用户名或密码错误!");
|
|
|
|
|
// 在请求属性中设置"loginid"属性,将用户输入的登录账号再次存入请求属性,方便在返回登录页面时可以回显给用户
|
|
|
|
|
request.setAttribute("loginid", loginid);
|
|
|
|
|
// 在请求属性中设置"loginpassword"属性,将用户输入的登录密码再次存入请求属性,方便在返回登录页面时可以回显给用户
|
|
|
|
|
request.setAttribute("loginpassword", loginpassword);
|
|
|
|
|
// 将请求转发到"/login.jsp"页面,返回登录页面并展示错误提示信息以及回显账号密码等内容
|
|
|
|
|
request.setAttribute("loginid",loginid);
|
|
|
|
|
request.setAttribute("loginpassword",loginpassword);
|
|
|
|
|
request.getRequestDispatcher("/login.jsp").forward(request, response);
|
|
|
|
|
}
|
|
|
|
|
} else if ("teacher".equals(roles)) {
|
|
|
|
|
// 创建一个Teacher对象,用于封装教师登录相关的信息,作为传递给业务逻辑层进行登录验证的数据对象
|
|
|
|
|
}else if ("teacher".equals(roles)) {
|
|
|
|
|
|
|
|
|
|
Teacher teacher = new Teacher();
|
|
|
|
|
// 设置教师对象的教师编号属性,使用前面获取到的id值
|
|
|
|
|
teacher.setT_id(id);
|
|
|
|
|
// 设置教师对象的密码属性,使用前面获取到的password值
|
|
|
|
|
teacher.setT_password(password);
|
|
|
|
|
|
|
|
|
|
// 创建TeacherService的实现类实例,用于调用业务逻辑层中与教师登录相关的方法
|
|
|
|
|
TeacherService service = new TeacherServiceImpl();
|
|
|
|
|
// 调用业务逻辑层的login方法,传入封装好的教师对象,进行登录验证,将验证通过后返回的教师对象赋值给loginTeacher变量(如果验证失败则为null)
|
|
|
|
|
loginTeacher = service.login(teacher);
|
|
|
|
|
|
|
|
|
|
// 判断登录验证是否成功,即loginTeacher不为null,表示找到了对应的教师记录,登录成功
|
|
|
|
|
if (loginTeacher!= null) {
|
|
|
|
|
// 将登录成功的教师对象存放到HttpSession中,属性名为"teacher",方便后续在其他页面判断用户是否登录以及获取教师相关信息
|
|
|
|
|
if (loginTeacher != null) {
|
|
|
|
|
session.setAttribute("teacher", loginTeacher);
|
|
|
|
|
// 在会话中设置"html_title"属性,值为"教师端",可能用于在页面上显示不同的标题栏等内容,标识当前处于教师端页面
|
|
|
|
|
session.setAttribute("html_title", "教师端");
|
|
|
|
|
// 重定向到"teacherIndexServlet",通常这个Servlet会将请求转发到教师端对应的首页(如"/WEB-INF/teacher/tIndex.jsp"等页面),实现登录成功后跳转到教师端首页的功能
|
|
|
|
|
// (此处注释掉了直接转发的代码,选择使用重定向方式)
|
|
|
|
|
// request.getRequestDispatcher("/WEB-INF/teacher/tIndex.jsp").forward(request, response);
|
|
|
|
|
response.sendRedirect("teacherIndexServlet");
|
|
|
|
|
} else {
|
|
|
|
|
// 如果登录验证失败,即loginTeacher为null,表示用户名或密码错误等情况
|
|
|
|
|
// 在请求属性中设置"login_msg"属性,添加提示信息,说明用户名或密码错误,用于后续在登录页面展示给用户
|
|
|
|
|
}else {
|
|
|
|
|
//登录失败 提示信息
|
|
|
|
|
request.setAttribute("login_msg", "用户名或密码错误!");
|
|
|
|
|
// 将请求转发到"/login.jsp"页面,返回登录页面并展示错误提示信息
|
|
|
|
|
request.getRequestDispatcher("/login.jsp").forward(request, response);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 创建一个Admin对象,用于封装管理员登录相关的信息,作为传递给业务逻辑层进行登录验证的数据对象
|
|
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
|
|
Admin admin = new Admin();
|
|
|
|
|
// 设置管理员对象的管理员编号属性,使用前面获取到的id值
|
|
|
|
|
admin.setA_id(id);
|
|
|
|
|
// 设置管理员对象的密码属性,使用前面获取到的password值
|
|
|
|
|
admin.setA_password(password);
|
|
|
|
|
|
|
|
|
|
// 创建AdminService的实现类实例,用于调用业务逻辑层中与管理员登录相关的方法
|
|
|
|
|
AdminService service = new AdminServiceImpl();
|
|
|
|
|
// 调用业务逻辑层的login方法,传入封装好的管理员对象,进行登录验证,将验证通过后返回的管理员对象赋值给loginAdmin变量(如果验证失败则为null)
|
|
|
|
|
loginAdmin = service.login(admin);
|
|
|
|
|
|
|
|
|
|
// 判断登录验证是否成功,即loginAdmin不为null,表示找到了对应的管理员记录,登录成功
|
|
|
|
|
if (loginAdmin!= null) {
|
|
|
|
|
// 将登录成功的管理员对象存放到HttpSession中,属性名为"admin",方便后续在其他页面判断用户是否登录以及获取管理员相关信息
|
|
|
|
|
if (loginAdmin != null) {
|
|
|
|
|
session.setAttribute("admin", loginAdmin);
|
|
|
|
|
// 在会话中设置"html_title"属性,值为"管理员",可能用于在页面上显示不同的标题栏等内容,标识当前处于管理员页面
|
|
|
|
|
session.setAttribute("html_title", "管理员");
|
|
|
|
|
// 重定向到"adminIndexServlet",通常这个Servlet会将请求转发到管理员端对应的首页(如"/WEB-INF/admin/aIndex.jsp"等页面),实现登录成功后跳转到管理员端首页的功能
|
|
|
|
|
// (此处注释掉了直接转发的代码,选择使用重定向方式)
|
|
|
|
|
// request.getRequestDispatcher("/WEB-INF/admin/aIndex.jsp").forward(request, response);
|
|
|
|
|
// request.getRequestDispatcher("/WEB-INF/admin/aIndex.jsp").forward(request,response);
|
|
|
|
|
response.sendRedirect("adminIndexServlet");
|
|
|
|
|
} else {
|
|
|
|
|
// 如果登录验证失败,即loginAdmin为null,表示用户名或密码错误等情况
|
|
|
|
|
// 在请求属性中设置"login_msg"属性,添加提示信息,说明用户名或密码错误,用于后续在登录页面展示给用户
|
|
|
|
|
}else {
|
|
|
|
|
//登录失败 提示信息
|
|
|
|
|
request.setAttribute("login_msg", "用户名或密码错误!");
|
|
|
|
|
// 将请求转发到"/login.jsp"页面,返回登录页面并展示错误提示信息
|
|
|
|
|
request.getRequestDispatcher("/login.jsp").forward(request, response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写doGet方法,在这个类中直接调用doPost方法来处理GET请求,意味着GET请求和POST请求在本Servlet中的处理逻辑是一样的,
|
|
|
|
|
// 即当接收到GET请求时,同样会执行上述doPost方法中的登录验证等相关逻辑
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
doPost(request, response);
|
|
|
|
|
doPost(request,response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|