|
|
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("/SchoAlterStuServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/SchoAlterStuServlet"
|
|
|
public class SchoAlterStuServlet 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 sno = req.getParameter("sno");// 学生学号
|
|
|
String sname = req.getParameter("sname");// 学生姓名
|
|
|
String ssex = req.getParameter("ssex");// 学生性别
|
|
|
String sage = req.getParameter("sage");// 学生年龄
|
|
|
String sclass = req.getParameter("sclass");// 学生班级
|
|
|
String specialty = req.getParameter("specialty");// 学生专业
|
|
|
String sdept = req.getParameter("sdept");// 学生所在系部
|
|
|
String sphone = req.getParameter("sphone");// 学生联系电话
|
|
|
String spsw = req.getParameter("spsw");// 学生密码
|
|
|
|
|
|
// 数据类型转换,将年龄从字符串转换为整数
|
|
|
int sage1 = Integer.parseInt(sage);
|
|
|
|
|
|
// 构造更新学生信息的SQL语句
|
|
|
String sql = "update student set sname = ?, ssex = ?, sage = ?, sclass = ?, specialty = ?, sdept = ?, sphone = ?, spsw = ? where sno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组
|
|
|
Object[] objects = {sname, ssex, sage1, sclass, specialty, sdept, sphone, spsw, sno};
|
|
|
|
|
|
// 调用DeptAdminDao的executeUpdate方法执行SQL更新操作,获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 打印受影响的行数,用于调试
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 转发请求到分页查询学生信息的Servlet,传递参数设置当前页为1,每页显示7条记录,其他查询条件为空
|
|
|
req.getRequestDispatcher("/SchoQueryStuByPageServlet?currentPage=1&rows=7&sname=&sclass=&sdept=").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
} |