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/schoadmin/SchoAddTeaServlet.java

89 lines
4.0 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.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);
}
}