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.
lsepidemicsituationsystem8/src/com/controller/frontweb/StuLoginServlet.java

104 lines
5.1 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.

package com.controller.frontweb;
// 声明该Servlet所在的包名为com.controller.frontweb
import com.dao.FrontWebDao;
// 导入数据访问对象类FrontWebDao用于执行数据库查询操作
import javax.servlet.ServletException;
// 导入Servlet异常类用于处理Servlet运行时可能出现的异常
import javax.servlet.annotation.WebServlet;
// 导入注解用于将Servlet映射到特定的URL
import javax.servlet.http.HttpServlet;
// 导入HttpServlet类所有基于HTTP协议的Servlet都继承自该类
import javax.servlet.http.HttpServletRequest;
// 导入用于处理HTTP请求的类通过它可以获取请求参数等信息
import javax.servlet.http.HttpServletResponse;
// 导入用于处理HTTP响应的类通过它可以设置响应内容、状态码等
import javax.servlet.http.HttpSession;
// 导入HttpSession类用于管理用户会话存储用户相关信息
import java.io.IOException;
// 导入用于处理I/O异常的类如在读写数据时可能发生的异常
import java.sql.ResultSet;
// 导入用于处理数据库查询结果的类
import java.sql.SQLException;
// 导入用于处理SQL异常的类
@WebServlet("/StuLoginServlet")
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuLoginServlet"
public class StuLoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 重写父类HttpServlet的doGet方法用于处理HTTP GET请求
// 设置请求的字符编码为UTF-8防止请求参数中的中文乱码
req.setCharacterEncoding("utf-8");
// 设置响应的字符编码为UTF-8防止响应内容中的中文乱码
resp.setCharacterEncoding("utf-8");
// 设置响应的内容类型为HTML格式并且字符编码为UTF-8告知客户端如何解析响应内容
resp.setContentType("text/html;charset=utf-8");
// 从HTTP请求的参数中获取名为"sno"的参数值,该值代表学生编号
String sno = req.getParameter("sno");
// 从HTTP请求的参数中获取名为"spsw"的参数值,该值代表学生密码
String spsw = req.getParameter("spsw");
// 将获取到的学生编号打印到控制台,用于调试
System.out.println(sno);
// 将获取到的学生密码打印到控制台,用于调试
System.out.println(spsw);
// 获取当前请求的会话对象,如果不存在则创建一个新的会话
HttpSession session = req.getSession();
// 将学生编号存储到会话中,以便在后续请求中可以获取该信息
session.setAttribute("sno", sno);
// 构造SQL查询语句用于从student表中查询匹配的学生记录
String sql = "select * from student where sno = ? and spsw = ?";
// 创建包含SQL语句参数的对象数组依次为学生编号、学生密码
Object[] objects = {sno, spsw};
// 调用FrontWebDao的login方法执行SQL查询并获取结果集
ResultSet resultSet = FrontWebDao.login(sql, objects);
try {
// 如果结果集有下一条记录,说明查询到了匹配的学生记录
if (resultSet.next()){
// 设置请求属性指定跳转的URL路径为"/StuInfoServlet"即学生信息Servlet
req.setAttribute("httpUrl","/StuInfoServlet");
// 设置请求属性,指定提示信息
req.setAttribute("info", "登录成功!即将跳转至学生信息页面!");
// 设置请求属性,指定页面标题
req.setAttribute("title","登录成功");
// 转发请求到指定的页面(/view/frontweb/info.jsp并传递请求和响应对象
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
}else {
// 如果结果集没有下一条记录,说明学号或密码错误
// 设置请求属性指定跳转回登录页面的URL路径
req.setAttribute("httpUrl","/view/frontweb/stulogin.jsp");
// 设置请求属性,指定错误提示信息
req.setAttribute("info", "登录失败!学号或密码错误!即将跳转至登录页面!");
// 设置请求属性,指定页面标题
req.setAttribute("title","登录失败");
// 转发请求到指定的页面(/view/frontweb/info.jsp并传递请求和响应对象
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
}
} catch (SQLException e) {
// 捕获SQL异常并打印异常堆栈信息用于调试和错误排查
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 重写父类HttpServlet的doPost方法用于处理HTTP POST请求
// 直接调用doGet方法来处理POST请求复用doGet方法中的业务逻辑
this.doGet(req, resp);
}
}