package com.controller.deptadmin;//包声明:类位于com.controller.deptadmin包中,归类管理部门相关的控制器。 import com.dao.DeptAdminDao;//包声明:类位于com.controller.deptadmin包中,归类管理部门相关的控制器。 import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/DeptAddTeaPunchServlet")//Servlet注解与类定义:映射URL路径/DeptAddTeaPunchServlet,继承HttpServlet处理HTTP请求。 public class DeptAddTeaPunchServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//处理GET请求:重写doGet方法,处理HTTP GET请求。 req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8");//统一请求和响应的字符编码为UTF-8,防止中文乱码,并设置响应内容类型为HTML //使用 req.getParameter("parameterName") 获取前端表单提交的每个参数的值 String tno = req.getParameter("tno");//教师编号 String tispunch = req.getParameter("tispunch");//是否打卡 String tpunchdate = req.getParameter("tpunchdate");//打卡日期 String tpunchtime = req.getParameter("tpunchtime");//打卡时间 String tishot = req.getParameter("tishot");//是否发热 String tiscough = req.getParameter("tiscough");//是否咳嗽 String tisseem = req.getParameter("tisseem");//是否见过人 String tisdiagnose = req.getParameter("tisdiagnose");//是否诊断 String tstatus = req.getParameter("tstatus");//状态 String sql = null; System.out.println("shgshgh");//这行代码是一个调试语句,打印 "shgshgh" 到控制台。它通常用于开发调试阶段,帮助开发者跟踪程序执行情况。可以在正式环境中移除。 //查询是否已打卡 sql = "select count(*) as num from teapunchin where tno = ? and tpunchdate = ?"; Object[] objects = {tno, tpunchdate};//构造一个 SQL 查询语句,检查数据库中是否已经存在该教师(tno)在指定日期(tpunchdate)的打卡记录。 int count = DeptAdminDao.findTotalCount(sql, objects);//使用 DeptAdminDao.findTotalCount() 方法执行查询,并返回匹配的记录数。count 表示查询结果,判断是否已有相同的打卡记录。 if (count == 0){//如果查询结果 count 为 0,表示该教师在该日期没有打卡记录,因此可以插入新的打卡记录。 sql = "insert into teapunchin values(?, ?, ?, ?, ?, ?, ?, ?, ?)"; Object[] objects1 = {tno, tispunch, tpunchdate, tpunchtime, tishot, tiscough, tisseem, tisdiagnose, tstatus}; //构造 SQL 插入语句,将教师的打卡信息插入到 teapunchin 表中。插入的字段包括:教师编号、是否打卡、打卡日期、打卡时间、是否发热、是否咳嗽、是否见过人、是否诊断、状态。 int num = DeptAdminDao.executeUpdate(sql, objects1);//使用 DeptAdminDao.executeUpdate() 方法执行插入操作,返回受影响的行数(即成功插入的记录数)。将插入操作的结果存储在 num 变量中。 System.out.println(num);//在控制台打印 num,用来调试输出插入操作是否成功,通常用于开发过程中查看插入操作是否成功。 req.getRequestDispatcher("/DeptQueryTeaPunchByPageServlet?currentPage=1&rows=7&tno=&tname=&tpunchdate=").forward(req, resp); }else {//如果插入操作成功,使用 req.getRequestDispatcher().forward() 方法将请求转发到另一个 Servlet (DeptQueryTeaPunchByPageServlet),用于分页查询教师的打卡记录。 req.getRequestDispatcher("/view/alluse/existdataofadd.jsp").forward(req, resp); } //如果查询到已经存在相同的打卡记录(count > 0),则转发请求到 existdataofadd.jsp 页面,提示用户该打卡记录已存在,无法再次添加。 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp);//doPost 方法处理 HTTP POST 请求。此处,doPost 方法调用 doGet 方法,使得 POST 请求和 GET 请求的处理逻辑一致,避免重复代码。 } }