@ -1,7 +1,8 @@
package com.controller.deptadmin ;
package com.controller.deptadmin ; //这行代码定义了当前类的包路径,表示该类属于 com.controller.deptadmin 包。
import com.dao.DeptAdminDao ;
import com.dao.DeptAdminDao ; //导入 DeptAdminDao 类,这是一个自定义的 DAO (数据访问对象) 类,提供了与数据库交互的操作方法。
//这些是从 javax.servlet 和 java.io 包导入的类。它们提供了与 Servlet 相关的功能,比如请求处理 (HttpServletRequest 和 HttpServletResponse) 和异常处理 (ServletException, IOException)。
import javax.servlet.ServletException ;
import javax.servlet.annotation.WebServlet ;
import javax.servlet.http.HttpServlet ;
@ -9,37 +10,52 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse ;
import java.io.IOException ;
@WebServlet ( "/DeptA lterStuPunchServlet")
public class DeptA lterStuPunch Servlet extends HttpServlet {
@WebServlet ( "/DeptA ddTeaServlet") //@WebServlet("/DeptAddTeaServlet") 是一个注解,表示这个 Servlet 会处理 URL 为 /DeptAddTeaServlet 的请求。
public class DeptA ddTea Servlet extends HttpServlet { //DeptAddTeaServlet 类继承自 HttpServlet, 意味着它是一个处理 HTTP 请求的 Servlet。
//重写 doGet 方法,处理 HTTP GET 请求。当浏览器发起 GET 请求时, Servlet 会调用该方法。
@Override
protected void doGet ( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
req . setCharacterEncoding ( "utf-8" ) ;
req . setCharacterEncoding ( "utf-8" ) ; //设置请求和响应的字符编码为 UTF-8, 确保中文字符能够正确处理, 避免乱码。
resp . setCharacterEncoding ( "utf-8" ) ;
resp . setContentType ( "text/html;charset=utf-8" ) ;
resp . setContentType ( "text/html;charset=utf-8" ) ; //设置响应的内容类型为 text/html, 并指定字符编码为 UTF-8, 这通常用于返回 HTML 页面。
//获取表单请求的参数
String sno = req . getParameter ( "sno" ) ;
String sispunch = req . getParameter ( "sispunch" ) ;
String spunchdate = req . getParameter ( "spunchdate" ) ;
String spunchtime = req . getParameter ( "spunchtime" ) ;
String sishot = req . getParameter ( "sishot" ) ;
String siscough = req . getParameter ( "siscough" ) ;
String sisseem = req . getParameter ( "sisseem" ) ;
String sisdiagnose = req . getParameter ( "sisdiagnose" ) ;
String sstatus = req . getParameter ( "sstatus" ) ;
String sql = "update stupunchin set sispunch = ?, spunchtime = ?, sishot = ?, siscough = ?, sisseem = ?, sisdiagnose = ?, sstatus = ? where sno = ? and spunchdate = ?" ;
Object [ ] objects = { sispunch , spunchtime , sishot , siscough , sisseem , sisdiagnose , sstatus , sno , spunchdate } ;
int num = DeptAdminDao . executeUpdate ( sql , objects ) ;
System . out . println ( num ) ;
req . getRequestDispatcher ( "/DeptQueryStuPunchByPageServlet?currentPage=1&rows=7&sname=&sclass=&spunchdate=" ) . forward ( req , resp ) ;
String tno = req . getParameter ( "tno" ) ; //获取表单中名为 "tno" 的参数值,并将其赋值给变量 tno。这个参数通常是教师编号。
String tname = req . getParameter ( "tname" ) ; //获取表单中名为 "tname" 的参数值,并将其赋值给变量 tname。这个参数通常是教师的姓名。
String tsex = req . getParameter ( "tsex" ) ; //获取表单中名为 "tsex" 的参数值,并将其赋值给变量 tsex。这个参数通常是教师的性别。
String tage = req . getParameter ( "tage" ) ; //获取表单中名为 "tage" 的参数值,并将其赋值给变量 tage。这个参数通常是教师的年龄, 类型是字符串。
String tdept = req . getParameter ( "tdept" ) ; //获取表单中名为 "tdept" 的参数值,并将其赋值给变量 tdept。这个参数通常是教师所属的部门。
String tphone = req . getParameter ( "tphone" ) ; //获取表单中名为 "tphone" 的参数值,并将其赋值给变量 tphone。这个参数通常是教师的电话。
String tpsw = req . getParameter ( "tpsw" ) ; //获取表单中名为 "tpsw" 的参数值,并将其赋值给变量 tpsw。这个参数通常是教师的密码。
//将 tage( 年龄) 从字符串转换为整数类型。因为表单中的数据都是字符串, 需要进行类型转换才能插入到数据库中。
int tage1 = Integer . parseInt ( tage ) ;
String sql = null ; //声明一个 SQL 语句字符串变量 sql, 用来存储后续的 SQL 查询语句。
System . out . println ( "shgshgh" ) ; //输出一个调试信息到控制台,用于验证代码是否正确执行。
//该段代码构造了一个 SQL 查询语句, 用于检查教师编号( tno) 是否已经存在于数据库中的 teacher 表。
sql = "select count(*) as num from teacher where tno = ?" ;
Object [ ] objects = { tno } ;
int count = DeptAdminDao . findTotalCount ( sql , objects ) ; //DeptAdminDao.findTotalCount(sql, objects) 是自定义的方法,它执行该 SQL 查询并返回结果,即该编号的教师是否存在。返回的是一个整数,表示该编号的教师数量。
if ( count = = 0 ) { //如果 count 为 0, 表示数据库中没有该教师编号的记录, 接下来的代码会执行插入操作。
sql = "insert into teacher values(?, ?, ?, ?, ?, ?, ?)" ; //构造 SQL 插入语句,将教师的各项信息插入到 teacher 表中。
Object [ ] objects1 = { tno , tname , tsex , tage1 , tdept , tphone , tpsw } ; //objects1 数组包含了插入教师数据所需的所有参数。
int num = DeptAdminDao . executeUpdate ( sql , objects1 ) ; //调用 DeptAdminDao.executeUpdate(sql, objects1) 执行插入操作。该方法执行 SQL 更新语句(包括插入、更新、删除),并返回受影响的记录数。
System . out . println ( num ) ; //输出插入操作的受影响的记录数,用于调试和验证。
/* req.getRequestDispatcher("/DeptQueryTeaByPageServlet?currentPage=1&rows=8").forward(req, resp);*/
//这里的代码会将请求转发到 DeptQueryTeaByPageServlet, 该 Servlet 用于显示教师列表的分页信息。请求被转发到新的 URL, 重新加载教师数据并显示给用户。这里的 currentPage=1&rows=7 表示请求第一页并每页显示 7 条记录。
req . getRequestDispatcher ( "/DeptQueryTeaByPageServlet?currentPage=1&rows=7&tno=&tname=&tsex=" ) . forward ( req , resp ) ;
} else {
req . getRequestDispatcher ( "/view/alluse/existdataofadd.jsp" ) . forward ( req , resp ) ; //如果 count 不为 0, 表示数据库中已经存在该编号的教师, 代码会将请求转发到 existdataofadd.jsp 页面,显示错误信息(例如“该教师编号已经存在”)。
}
}
//重写 doPost 方法,以便当接收到 POST 请求时,调用 doGet 方法进行处理。这是因为这两个方法的逻辑相同,都是处理添加教师的功能。
@Override
protected void doPost ( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
doGet ( req , resp ) ;