package com.controller.schoadmin; // 声明该类所在的包名为com.controller.schoadmin import com.dao.DeptAdminDao; // 导入数据访问对象类,用于执行数据库的查询和更新操作 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 java.io.IOException; // 导入用于处理I/O异常的类,如在读写数据时可能发生的异常 @WebServlet("/SchoAddTeaServlet") // 使用@WebServlet注解将该Servlet映射到URL路径为"/SchoAddTeaServlet" public class SchoAddTeaServlet 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"); // 获取表单请求的参数 String tno = req.getParameter("tno");// 教师编号 String tname = req.getParameter("tname");// 教师姓名 String tsex = req.getParameter("tsex");// 教师性别 String tage = req.getParameter("tage");// 教师年龄 String tdept = req.getParameter("tdept");// 教师所在系部 String tphone = req.getParameter("tphone");// 教师联系电话 String tpsw = req.getParameter("tpsw");// 教师密码 // 数据类型转换,将年龄从字符串转换为整数 int tage1 = Integer.parseInt(tage); // 声明SQL语句变量 String sql = null; // 打印调试信息 System.out.println("shgshgh"); // 查询是否存在该教师(根据教师编号查询) sql = "select count(*) as num from teacher where tno = ?"; Object[] objects = {tno}; // 调用DeptAdminDao的方法执行SQL查询,获取符合条件的记录数量 int count = DeptAdminDao.findTotalCount(sql, objects); if (count == 0) { // 如果不存在该教师记录 // 构造插入教师信息的SQL语句 sql = "insert into teacher values(?, ?, ?, ?, ?, ?, ?)"; Object[] objects1 = {tno, tname, tsex, tage1, tdept, tphone, tpsw}; // 调用DeptAdminDao的方法执行SQL插入操作,获取受影响的行数 int num = DeptAdminDao.executeUpdate(sql, objects1); // 打印插入操作影响的行数,用于调试 System.out.println(num); // 转发请求到分页查询教师信息的Servlet,传递参数设置当前页为1,每页显示7条记录,其他查询条件为空 req.getRequestDispatcher("/SchoQueryTeaByPageServlet?currentPage=1&rows=7&tno=&tname=&tdept=").forward(req, resp); } else { // 如果教师记录已存在,转发请求到提示已存在数据的页面 req.getRequestDispatcher("/view/alluse/existdataofadd.jsp").forward(req, resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求 // 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑 doGet(req, resp); } }