|
|
package com.controller.alluse;
|
|
|
|
|
|
import com.alibaba.druid.util.JdbcUtils;
|
|
|
import com.dao.SchoAdminDao;
|
|
|
import com.dao.StuDao;
|
|
|
import com.dao.TeaDao;
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
|
/**
|
|
|
* 处理用户登录请求的Servlet
|
|
|
* 根据用户选择的身份(学生/教师/管理员)进行不同的验证逻辑
|
|
|
*/
|
|
|
@WebServlet("/LoginServlet")
|
|
|
public class LoginServlet extends HttpServlet {
|
|
|
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求和响应的字符编码,防止中文乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从请求参数中获取用户输入的用户名、密码和身份
|
|
|
String userName = req.getParameter("userName");
|
|
|
String psw = req.getParameter("psw");
|
|
|
String identity = req.getParameter("identity1");
|
|
|
// 打印日志用于调试
|
|
|
System.out.println(userName);
|
|
|
System.out.println(psw);
|
|
|
System.out.println(identity);
|
|
|
|
|
|
// 根据用户选择的身份进行不同的验证逻辑
|
|
|
if (identity.equals("学生")){//学生身份
|
|
|
boolean flag = false; // 标记是否验证成功
|
|
|
String sql = "select * from student where sname = ? and spsw = ?"; // SQL查询语句
|
|
|
Object[] objects = {userName, psw}; // SQL查询参数
|
|
|
ResultSet resultSet = StuDao.login(sql, objects); // 执行数据库查询
|
|
|
String sno = null; // 学生学号
|
|
|
try {
|
|
|
if (resultSet.next()){ // 如果查询结果不为空
|
|
|
flag = true; // 验证成功
|
|
|
sno = resultSet.getString("sno"); // 获取学生学号
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
}finally {
|
|
|
JdbcUtils.close(resultSet); // 关闭结果集,释放资源
|
|
|
}
|
|
|
if (flag){ // 验证成功
|
|
|
System.out.println("登录成功!");
|
|
|
// 创建会话,存储用户信息
|
|
|
HttpSession session = req.getSession();
|
|
|
session.setAttribute("userName", userName); // 存储用户名
|
|
|
session.setAttribute("sno", sno); // 存储学号
|
|
|
System.out.println(sno);
|
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/stu/stumainview.jsp");
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
req.setAttribute("title","登录成功");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/stu/stumainview.jsp");
|
|
|
}else { // 验证失败
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
// 设置错误信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
req.setAttribute("title","登录失败");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
}
|
|
|
}else if (identity.equals("教师")){//教师身份
|
|
|
boolean flag = false;
|
|
|
String sql = "select * from teacher where tname = ? and tpsw = ?";
|
|
|
Object[] objects = {userName, psw};
|
|
|
ResultSet resultSet = TeaDao.login(sql, objects);
|
|
|
String tno = null;
|
|
|
try {
|
|
|
if (resultSet.next()){
|
|
|
flag = true;
|
|
|
tno = resultSet.getString("tno");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
|
JdbcUtils.close(resultSet);
|
|
|
}
|
|
|
if (flag){
|
|
|
System.out.println("登录成功!");
|
|
|
// 创建会话,存储用户信息
|
|
|
HttpSession session = req.getSession();
|
|
|
session.setAttribute("userName", userName);
|
|
|
session.setAttribute("tno", tno);
|
|
|
System.out.println(tno);
|
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/tea/teamainview.jsp");
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
req.setAttribute("title","登录成功");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/guide/mainview_guide.jsp");
|
|
|
}else {
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
// 设置错误信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
req.setAttribute("title","登录失败");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
}
|
|
|
}else {//如果是管理员
|
|
|
boolean flag = false;
|
|
|
String sql = "select * from admin where adname = ? and adpsw = ? and belong = ?";
|
|
|
Object[] objects = {userName, psw, identity};
|
|
|
ResultSet resultSet = SchoAdminDao.login(sql, objects);
|
|
|
try {
|
|
|
if (resultSet.next()){
|
|
|
flag = true;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
|
JdbcUtils.close(resultSet);
|
|
|
}
|
|
|
|
|
|
if (flag){ // 管理员验证成功
|
|
|
|
|
|
if (identity.equals("学校")){//学校管理员
|
|
|
System.out.println("登录成功!");
|
|
|
HttpSession session = req.getSession();
|
|
|
session.setAttribute("belong", identity); // 存储管理员所属单位
|
|
|
System.out.println(identity);
|
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/schoadmin/schomainview.jsp");
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
req.setAttribute("title","登录成功");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
}else {//二级学院管理员
|
|
|
System.out.println("登录成功!");
|
|
|
// 创建会话,存储用户信息
|
|
|
HttpSession session = req.getSession();
|
|
|
session.setAttribute("belong", identity);
|
|
|
System.out.println(identity);
|
|
|
|
|
|
// 设置跳转信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/deptadmin/deptmainview.jsp");
|
|
|
req.setAttribute("info", "登录成功!即将跳转至后台首页!");
|
|
|
req.setAttribute("title","登录成功");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
// resp.sendRedirect(req.getContextPath() + "/view/guide/mainview_guide.jsp");
|
|
|
}
|
|
|
|
|
|
}else {
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
// 设置错误信息并转发到提示页面
|
|
|
req.setAttribute("httpUrl","/view/login/login.jsp");
|
|
|
req.setAttribute("info", "登录失败!用户名或密码错误!即将跳转至登录页面!");
|
|
|
req.setAttribute("title","登录失败");
|
|
|
req.getRequestDispatcher("/view/alluse/info.jsp").forward(req, resp);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp); // 对于POST请求,直接调用doGet方法处理
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
|
|
|
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;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
|
* 处理新闻删除请求的Servlet
|
|
|
* 根据新闻ID删除对应新闻记录
|
|
|
*/
|
|
|
@WebServlet("/DelNewsServlet")
|
|
|
public class DelNewsServlet extends HttpServlet {
|
|
|
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求和响应的字符编码,防止中文乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从请求参数中获取要删除的新闻ID
|
|
|
String nid = req.getParameter("nid");
|
|
|
int nid1 = Integer.parseInt(nid); // 将字符串类型的ID转换为整数
|
|
|
|
|
|
// 打印日志用于调试
|
|
|
System.out.println(nid1);
|
|
|
|
|
|
String sql = null; // SQL语句变量
|
|
|
|
|
|
// 第一步:检查新闻是否存在
|
|
|
sql = "select count(*) as num from news where nid = ?";
|
|
|
Object[] objects = {nid1}; // SQL查询参数
|
|
|
int num = FrontWebDao.findTotalCount(sql, objects); // 执行查询,获取新闻数量
|
|
|
System.out.println(num);
|
|
|
|
|
|
if (num > 0){ // 如果新闻存在,则执行删除操作
|
|
|
sql = "delete from news where nid = ?";
|
|
|
Object[] objects1 = {nid1};
|
|
|
int num1 = FrontWebDao.executeUpdate(sql, objects1); // 执行删除操作
|
|
|
|
|
|
if (num1 > 0){ // 删除成功
|
|
|
// 通过JavaScript弹出提示框并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('删除成功!'); window.location='" + req.getContextPath() + "/view/frontweb/delnews.jsp';" + "window.close();</script>");
|
|
|
}else { // 删除失败(可能是数据库操作异常)
|
|
|
resp.getWriter().write("<script>alert('删除失败!'); window.location='" + req.getContextPath() + "/view/frontweb/delnews.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else { // 新闻不存在,不能删除
|
|
|
resp.getWriter().write("<script>alert('删除失败!不存在此新闻编号!'); window.location='" + req.getContextPath() + "/view/frontweb/delnews.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
this.doGet(req, resp); // 对于POST请求,直接调用doGet方法处理
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* 处理部门管理员密码修改请求的Servlet
|
|
|
* 验证两次输入的新密码是否一致并更新数据库
|
|
|
*/
|
|
|
@WebServlet("/DeptAdmAlterPswServlet")
|
|
|
public class DeptAdmAlterPswServlet extends HttpServlet {
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求和响应的字符编码,防止中文乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从会话中获取管理员编号(标识当前登录用户)
|
|
|
HttpSession session = req.getSession();
|
|
|
String adno = (String) session.getAttribute("adno");
|
|
|
|
|
|
// 从请求参数中获取用户输入的新密码和确认密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (adpsw.equals(adpsw1)){// 相同则进行修改操作
|
|
|
String sql = "update admin set adpsw = ? where adno = ?"; // SQL更新语句
|
|
|
Object[] objects = {adpsw, adno}; // SQL参数:新密码和管理员编号
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects); // 执行数据库更新操作
|
|
|
|
|
|
if (num > 0){ // 更新成功
|
|
|
// 提示用户修改成功并要求重新登录
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请重新登录!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else { // 更新失败(可能是数据库操作异常)
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmalterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {// 两次密码不同,提示用户重新输入
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmalterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
this.doGet(req, resp); // 对于POST请求,直接调用doGet方法处理
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 处理部门管理员忘记密码重置请求的Servlet
|
|
|
* 验证两次输入的新密码是否一致并更新数据库
|
|
|
*/
|
|
|
@WebServlet("/DeptAdmForgetPswServlet")
|
|
|
public class DeptAdmForgetPswServlet extends HttpServlet {
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求和响应的字符编码,防止中文乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从请求参数中获取管理员编号(用户输入的需要重置密码的账号)
|
|
|
String adno = req.getParameter("adno");
|
|
|
|
|
|
// 从请求参数中获取用户输入的新密码和确认密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (adpsw.equals(adpsw1)){// 相同则进行修改操作
|
|
|
String sql = "update admin set adpsw = ? where adno = ?"; // SQL更新语句
|
|
|
Object[] objects = {adpsw, adno}; // SQL参数:新密码和管理员编号
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects); // 执行数据库更新操作
|
|
|
|
|
|
if (num > 0){ // 更新成功
|
|
|
// 提示用户修改成功并跳转到登录页面
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else { // 更新失败(可能是管理员编号不存在或数据库操作异常)
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {// 两次密码不同,提示用户重新输入
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
this.doGet(req, resp); // 对于POST请求,直接调用doGet方法处理
|
|
|
}
|
|
|
}package com.controller.frontweb;
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 处理部门管理员忘记密码重置请求的Servlet
|
|
|
* 验证两次输入的新密码是否一致并更新数据库
|
|
|
*/
|
|
|
@WebServlet("/DeptAdmForgetPswServlet")
|
|
|
public class DeptAdmForgetPswServlet extends HttpServlet {
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求和响应的字符编码,防止中文乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从请求参数中获取管理员编号(用户输入的需要重置密码的账号)
|
|
|
String adno = req.getParameter("adno");
|
|
|
|
|
|
// 从请求参数中获取用户输入的新密码和确认密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 输入验证:检查参数是否为空
|
|
|
if (adno == null || adpsw == null || adpsw1 == null) {
|
|
|
resp.getWriter().write("<script>alert('参数不能为空!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (adpsw.equals(adpsw1)){// 相同则进行修改操作
|
|
|
// 验证密码强度(示例:至少8位,包含字母和数字)
|
|
|
if (!isPasswordValid(adpsw)) {
|
|
|
resp.getWriter().write("<script>alert('密码强度不足!至少8位,包含字母和数字'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
String sql = "update admin set adpsw = ? where adno = ?"; // SQL更新语句
|
|
|
Object[] objects = {adpsw, adno}; // SQL参数:新密码和管理员编号
|
|
|
|
|
|
// 验证管理员是否存在
|
|
|
if (!isAdminExists(adno)) {
|
|
|
resp.getWriter().write("<script>alert('管理员账号不存在!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects); // 执行数据库更新操作
|
|
|
|
|
|
if (num > 0){ // 更新成功
|
|
|
// 提示用户修改成功并跳转到登录页面
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else { // 更新失败
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请联系管理员'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
}
|
|
|
}else {// 两次密码不同,提示用户重新输入
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
this.doGet(req, resp); // 对于POST请求,直接调用doGet方法处理
|
|
|
}
|
|
|
|
|
|
// 验证管理员是否存在
|
|
|
private boolean isAdminExists(String adno) {
|
|
|
String sql = "select count(*) as num from admin where adno = ?";
|
|
|
Object[] params = {adno};
|
|
|
int count = FrontWebDao.findTotalCount(sql, params);
|
|
|
return count > 0;
|
|
|
}
|
|
|
|
|
|
// 验证密码强度
|
|
|
private boolean isPasswordValid(String password) {
|
|
|
// 至少8位,包含字母和数字
|
|
|
return password.matches("^(?=.*[A-Za-z])(?=.*\\d).{8,}$");
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 处理部门管理员忘记密码重置请求的Servlet
|
|
|
* 验证两次输入的新密码是否一致并更新数据库
|
|
|
*/
|
|
|
@WebServlet("/DeptAdmForgetPswServlet")
|
|
|
public class DeptAdmForgetPswServlet extends HttpServlet {
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求的字符编码为UTF-8,防止中文乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
// 设置响应的字符编码为UTF-8,防止中文乱码
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
// 设置响应的内容类型为HTML,字符编码为UTF-8
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从请求参数中获取管理员编号(用户输入的需要重置密码的账号)
|
|
|
String adno = req.getParameter("adno");
|
|
|
|
|
|
// 从请求参数中获取用户输入的新密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
// 从请求参数中获取用户输入的确认密码
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 输入验证:检查获取到的管理员编号、新密码、确认密码参数是否为空
|
|
|
if (adno == null || adpsw == null || adpsw1 == null) {
|
|
|
// 如果参数为空,通过响应输出一段JavaScript代码,弹出提示框显示“参数不能为空!”
|
|
|
// 并将页面重定向到密码重置页面(/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('参数不能为空!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前方法的执行
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (adpsw.equals(adpsw1)){// 相同则进行修改操作
|
|
|
// 调用isPasswordValid方法验证密码强度(示例:至少8位,包含字母和数字)
|
|
|
if (!isPasswordValid(adpsw)) {
|
|
|
// 如果密码强度不足,通过响应输出一段JavaScript代码,弹出提示框显示“密码强度不足!至少8位,包含字母和数字”
|
|
|
// 并将页面重定向到密码重置页面(/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('密码强度不足!至少8位,包含字母和数字'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前方法的执行
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 构造SQL更新语句,用于更新管理员表中指定管理员编号的密码
|
|
|
String sql = "update admin set adpsw = ? where adno = ?";
|
|
|
// 设置SQL语句的参数,第一个参数为新密码,第二个参数为管理员编号
|
|
|
Object[] objects = {adpsw, adno};
|
|
|
|
|
|
// 调用isAdminExists方法验证管理员是否存在
|
|
|
if (!isAdminExists(adno)) {
|
|
|
// 如果管理员账号不存在,通过响应输出一段JavaScript代码,弹出提示框显示“管理员账号不存在!”
|
|
|
// 并将页面重定向到密码重置页面(/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('管理员账号不存在!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前方法的执行
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 调用FrontWebDao的executeUpdate方法执行SQL更新语句,返回受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 如果受影响的行数大于0,说明更新成功
|
|
|
if (num > 0){
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码成功!请登录!”
|
|
|
// 并将页面重定向到部门管理员登录页面(/view/frontweb/deptadmlogin.jsp),同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else { // 如果受影响的行数不大于0,说明更新失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码失败!请联系管理员”
|
|
|
// 并将页面重定向到密码重置页面(/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请联系管理员'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
}
|
|
|
}else {// 如果两次密码不同
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“两次密码不一样!请重新输入密码!”
|
|
|
// 并将页面重定向到密码重置页面(/view/frontweb/deptadmforgetpsw.jsp),同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 对于POST请求,直接调用doGet方法进行处理
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
|
|
|
// 验证管理员是否存在的私有方法
|
|
|
private boolean isAdminExists(String adno) {
|
|
|
// 构造SQL查询语句,统计管理员表中指定管理员编号的记录数量
|
|
|
String sql = "select count(*) as num from admin where adno = ?";
|
|
|
// 设置SQL语句的参数为管理员编号
|
|
|
Object[] params = {adno};
|
|
|
// 调用FrontWebDao的findTotalCount方法执行SQL查询语句,获取记录数量
|
|
|
int count = FrontWebDao.findTotalCount(sql, params);
|
|
|
// 如果记录数量大于0,返回true,否则返回false
|
|
|
return count > 0;
|
|
|
}
|
|
|
|
|
|
// 验证密码强度的私有方法
|
|
|
private boolean isPasswordValid(String password) {
|
|
|
// 使用正则表达式判断密码是否至少8位,并且包含字母和数字
|
|
|
return password.matches("^(?=.*[A-Za-z])(?=.*\\d).{8,}$");
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 处理部门管理员忘记密码重置请求的Servlet
|
|
|
* 验证两次输入的新密码是否一致并更新数据库
|
|
|
*/
|
|
|
@WebServlet("/DeptAdmForgetPswServlet")
|
|
|
public class DeptAdmForgetPswServlet extends HttpServlet {
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求的字符编码为UTF-8,确保能正确处理请求中的中文字符等,防止乱码
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
// 设置响应的字符编码为UTF-8,保证响应内容在传输过程中字符的正确性,防止乱码
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
// 设置响应的内容类型为HTML格式,且字符编码为UTF-8,告知客户端如何解析响应内容
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"adno"的参数值,该值代表管理员编号(用户输入的需要重置密码的账号)
|
|
|
String adno = req.getParameter("adno");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"adpsw"的参数值,该值代表用户输入的新密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
// 从HTTP请求的参数中获取名为"adpsw1"的参数值,该值代表用户输入的用于确认的新密码
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 进行输入验证,检查获取到的管理员编号、新密码、确认密码这三个参数是否有任何一个为null
|
|
|
if (adno == null || adpsw == null || adpsw1 == null) {
|
|
|
// 如果存在参数为null的情况,通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“参数不能为空!”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('参数不能为空!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前doGet方法的执行,不再继续后续操作
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 判断用户输入的新密码和确认密码是否相等
|
|
|
if (adpsw.equals(adpsw1)){
|
|
|
// 如果相等,则进行密码强度验证,调用isPasswordValid方法来检查密码是否符合要求
|
|
|
if (!isPasswordValid(adpsw)) {
|
|
|
// 如果密码强度不符合要求(例如长度不足8位或不包含字母和数字)
|
|
|
// 通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“密码强度不足!至少8位,包含字母和数字”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('密码强度不足!至少8位,包含字母和数字'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前doGet方法的执行,不再继续后续操作
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 构造一条SQL更新语句,用于更新数据库中admin表的记录
|
|
|
// 将指定管理员编号(adno)对应的密码(adpsw)进行更新
|
|
|
String sql = "update admin set adpsw = ? where adno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是管理员编号
|
|
|
Object[] objects = {adpsw, adno};
|
|
|
|
|
|
// 调用isAdminExists方法来验证要重置密码的管理员账号是否存在于数据库中
|
|
|
if (!isAdminExists(adno)) {
|
|
|
// 如果管理员账号不存在,通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“管理员账号不存在!”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('管理员账号不存在!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前doGet方法的执行,不再继续后续操作
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 调用FrontWebDao类中的executeUpdate方法执行SQL更新语句
|
|
|
// 该方法会返回受影响的行数,即成功更新的记录数量,将其存储在num变量中
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 判断受影响的行数是否大于0,若大于0表示密码更新操作成功
|
|
|
if (num > 0){
|
|
|
// 如果更新成功,通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“修改密码成功!请登录!”
|
|
|
// 并将页面重定向到部门管理员登录页面(路径为/view/frontweb/deptadmlogin.jsp),同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码更新操作失败
|
|
|
// 通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“修改密码失败!请联系管理员”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请联系管理员'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果用户输入的新密码和确认密码不相等
|
|
|
// 通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“两次密码不一样!请重新输入密码!”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp),同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 对于接收到的POST请求,直接调用doGet方法来处理,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
|
|
|
// 私有方法,用于验证管理员账号是否存在于数据库中
|
|
|
private boolean isAdminExists(String adno) {
|
|
|
// 构造一条SQL查询语句,用于统计admin表中指定管理员编号(adno)的记录数量
|
|
|
String sql = "select count(*) as num from admin where adno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,参数为管理员编号
|
|
|
Object[] params = {adno};
|
|
|
// 调用FrontWebDao类中的findTotalCount方法执行SQL查询语句
|
|
|
// 该方法会返回查询结果中记录的数量,将其存储在count变量中
|
|
|
int count = FrontWebDao.findTotalCount(sql, params);
|
|
|
// 如果记录数量大于0,说明管理员账号存在,返回true;否则返回false
|
|
|
return count > 0;
|
|
|
}
|
|
|
|
|
|
// 私有方法,用于验证密码强度是否符合要求
|
|
|
private boolean isPasswordValid(String password) {
|
|
|
// 使用正则表达式进行匹配,判断密码是否至少8位,并且同时包含字母和数字
|
|
|
// 如果匹配成功,说明密码强度符合要求,返回true;否则返回false
|
|
|
return password.matches("^(?=.*[A-Za-z])(?=.*\\d).{8,}$");
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库相关操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入处理Servlet异常的类ServletException
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入用于将Servlet映射到URL的注解WebServlet
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,是所有基于HTTP协议的Servlet的基类
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类HttpServletRequest
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类HttpServletResponse
|
|
|
import java.io.IOException;
|
|
|
// 导入处理I/O异常的类IOException
|
|
|
|
|
|
/**
|
|
|
* 处理部门管理员忘记密码重置请求的Servlet
|
|
|
* 验证两次输入的新密码是否一致并更新数据库
|
|
|
*/
|
|
|
@WebServlet("/DeptAdmForgetPswServlet")
|
|
|
// 使用WebServlet注解将该Servlet映射到URL路径"/DeptAdmForgetPswServlet"
|
|
|
public class DeptAdmForgetPswServlet 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");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"adno"的参数值,该值代表管理员编号(用户输入的需要重置密码的账号)
|
|
|
String adno = req.getParameter("adno");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"adpsw"的参数值,该值代表用户输入的新密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
// 从HTTP请求的参数中获取名为"adpsw1"的参数值,该值代表用户输入的用于确认的新密码
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 进行输入验证,检查获取到的管理员编号、新密码、确认密码这三个参数是否有任何一个为null
|
|
|
if (adno == null || adpsw == null || adpsw1 == null) {
|
|
|
// 如果存在参数为null的情况,通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“参数不能为空!”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('参数不能为空!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前doGet方法的执行,不再继续后续操作
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 判断用户输入的新密码和确认密码是否相等
|
|
|
if (adpsw.equals(adpsw1)){
|
|
|
// 如果相等,则进行密码强度验证,调用isPasswordValid方法来检查密码是否符合要求
|
|
|
if (!isPasswordValid(adpsw)) {
|
|
|
// 如果密码强度不符合要求(例如长度不足8位或不包含字母和数字)
|
|
|
// 通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“密码强度不足!至少8位,包含字母和数字”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('密码强度不足!至少8位,包含字母和数字'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前doGet方法的执行,不再继续后续操作
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 构造一条SQL更新语句,用于更新数据库中admin表的记录
|
|
|
// 将指定管理员编号(adno)对应的密码(adpsw)进行更新
|
|
|
String sql = "update admin set adpsw = ? where adno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是管理员编号
|
|
|
Object[] objects = {adpsw, adno};
|
|
|
|
|
|
// 调用isAdminExists方法来验证要重置密码的管理员账号是否存在于数据库中
|
|
|
if (!isAdminExists(adno)) {
|
|
|
// 如果管理员账号不存在,通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“管理员账号不存在!”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('管理员账号不存在!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
// 终止当前doGet方法的执行,不再继续后续操作
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 调用FrontWebDao类中的executeUpdate方法执行SQL更新语句
|
|
|
// 该方法会返回受影响的行数,即成功更新的记录数量,将其存储在num变量中
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 判断受影响的行数是否大于0,若大于0表示密码更新操作成功
|
|
|
if (num > 0){
|
|
|
// 如果更新成功,通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“修改密码成功!请登录!”
|
|
|
// 并将页面重定向到部门管理员登录页面(路径为/view/frontweb/deptadmlogin.jsp),同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码更新操作失败
|
|
|
// 通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“修改密码失败!请联系管理员”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp)
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请联系管理员'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果用户输入的新密码和确认密码不相等
|
|
|
// 通过响应向客户端发送一段JavaScript代码
|
|
|
// 该代码会弹出一个提示框,显示“两次密码不一样!请重新输入密码!”
|
|
|
// 并将页面重定向到密码重置页面(路径为/view/frontweb/deptadmforgetpsw.jsp),同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/deptadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,处理HTTP POST请求
|
|
|
// 对于接收到的POST请求,直接调用doGet方法来处理,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
|
|
|
// 私有方法,用于验证管理员账号是否存在于数据库中
|
|
|
private boolean isAdminExists(String adno) {
|
|
|
// 构造一条SQL查询语句,用于统计admin表中指定管理员编号(adno)的记录数量
|
|
|
String sql = "select count(*) as num from admin where adno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,参数为管理员编号
|
|
|
Object[] params = {adno};
|
|
|
// 调用FrontWebDao类中的findTotalCount方法执行SQL查询语句
|
|
|
// 该方法会返回查询结果中记录的数量,将其存储在count变量中
|
|
|
int count = FrontWebDao.findTotalCount(sql, params);
|
|
|
// 如果记录数量大于0,说明管理员账号存在,返回true;否则返回false
|
|
|
return count > 0;
|
|
|
}
|
|
|
|
|
|
// 私有方法,用于验证密码强度是否符合要求
|
|
|
private boolean isPasswordValid(String password) {
|
|
|
// 使用正则表达式进行匹配,判断密码是否至少8位,并且同时包含字母和数字
|
|
|
// 如果匹配成功,说明密码强度符合要求,返回true;否则返回false
|
|
|
return password.matches("^(?=.*[A-Za-z])(?=.*\\d).{8,}$");
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库相关操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类
|
|
|
|
|
|
@WebServlet("/SchoAdmAlterPswServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/SchoAdmAlterPswServlet"
|
|
|
public class SchoAdmAlterPswServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"adno"的属性值,并将其转换为String类型,该值表示管理员编号
|
|
|
String adno = (String) session.getAttribute("adno");
|
|
|
|
|
|
// 从请求参数中获取名为"adpsw"的参数值,该值表示用户输入的新密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
// 从请求参数中获取名为"adpsw1"的参数值,该值表示用户再次输入的新密码用于确认
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (adpsw.equals(adpsw1)){
|
|
|
// 如果相同,则进行密码修改操作
|
|
|
// 构造SQL更新语句,用于更新admin表中指定adno的管理员的密码
|
|
|
String sql = "update admin set adpsw = ? where adno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是管理员编号
|
|
|
Object[] objects = {adpsw, adno};
|
|
|
// 调用FrontWebDao的executeUpdate方法执行SQL更新语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
if (num > 0){
|
|
|
// 如果受影响的行数大于0,说明密码修改成功
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示修改密码成功,并要求用户重新登录
|
|
|
// 同时将页面重定向到学校管理员登录页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请重新登录!'); window.location='" + req.getContextPath() + "/view/frontweb/schoadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码修改失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示修改密码失败,并让用户重新输入密码
|
|
|
// 同时将页面重定向到学校管理员修改密码页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/schoadmalterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果两次输入的密码不同
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示两次密码不一样,并让用户重新输入密码
|
|
|
// 同时将页面重定向到学校管理员修改密码页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/schoadmalterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所属的包为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于后续执行数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常类,用于处理Servlet运行时可能出现的异常
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入注解,用于将Servlet映射到特定的URL
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,这是编写Servlet时的基础类,用于处理HTTP请求和响应
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类,通过它可以获取请求参数等信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类,通过它可以设置响应内容、状态码等
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,例如在读写数据时可能出现的异常
|
|
|
|
|
|
@WebServlet("/SchoAdmForgetPswServlet")
|
|
|
// 使用@WebServlet注解将当前Servlet映射到URL路径为"/SchoAdmForgetPswServlet"
|
|
|
public class SchoAdmForgetPswServlet 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");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"adno"的参数值,该值代表管理员编号
|
|
|
String adno = req.getParameter("adno");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"adpsw"的参数值,该值代表用户输入的新密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
// 从HTTP请求的参数中获取名为"adpsw1"的参数值,该值代表用户再次输入的用于确认的新密码
|
|
|
String adpsw1 = req.getParameter("adpsw1");
|
|
|
|
|
|
// 判断用户输入的新密码和确认密码是否相等
|
|
|
if (adpsw.equals(adpsw1)){
|
|
|
// 如果相等,则进行密码修改操作
|
|
|
// 构造SQL更新语句,用于更新数据库中admin表的记录,将指定管理员编号(adno)对应的密码(adpsw)进行更新
|
|
|
String sql = "update admin set adpsw = ? where adno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是管理员编号
|
|
|
Object[] objects = {adpsw, adno};
|
|
|
// 调用FrontWebDao类中的executeUpdate方法执行SQL更新语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
if (num > 0){
|
|
|
// 如果受影响的行数大于0,说明密码更新操作成功
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码成功!请登录!”
|
|
|
// 并将页面重定向到学校管理员登录页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/schoadmlogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码更新操作失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码失败!请重新输入密码!”
|
|
|
// 并将页面重定向到学校管理员忘记密码重置页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/schoadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果用户输入的新密码和确认密码不相等
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“两次密码不一样!请重新输入密码!”
|
|
|
// 并将页面重定向到学校管理员忘记密码重置页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/schoadmforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库相关操作,如查询等
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import javax.sql.rowset.serial.SerialStruct;
|
|
|
// 导入SerialStruct类,这里未使用到,属于无用导入(可删除)
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入处理I/O异常的类,如在读取或写入数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类,用于获取查询到的数据
|
|
|
|
|
|
@WebServlet("/SchoAdmLoginServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/SchoAdmLoginServlet"
|
|
|
public class SchoAdmLoginServlet 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 adno = req.getParameter("adno");
|
|
|
// 获取表单请求中的参数,这里获取管理员密码
|
|
|
String adpsw = req.getParameter("adpsw");
|
|
|
// 固定设置所属单位为"学校",表示学校管理员
|
|
|
String belong = "学校";
|
|
|
|
|
|
// 构造SQL查询语句,用于从admin表中查询匹配的管理员记录
|
|
|
String sql = "select * from admin where adno = ? and adpsw = ? and belong = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为管理员编号、密码、所属单位
|
|
|
Object[] objects = {adno, adpsw, belong};
|
|
|
// 调用FrontWebDao的login方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = FrontWebDao.login(sql, objects);
|
|
|
|
|
|
// 判断密码和账号是否正确
|
|
|
try {
|
|
|
// 如果结果集有下一条记录,说明查询到了匹配的管理员记录
|
|
|
if (resultSet.next()){
|
|
|
// 创建会话对象,用于存储用户会话信息
|
|
|
HttpSession session = req.getSession();
|
|
|
// 在会话中存储管理员编号
|
|
|
session.setAttribute("adno", adno);
|
|
|
// 在会话中存储所属单位(这里是"学校")
|
|
|
session.setAttribute("belong", belong);
|
|
|
// 在会话中存储学校管理员编号(与adno相同,这里可能存在命名重复问题)
|
|
|
session.setAttribute("schoadno", adno);
|
|
|
|
|
|
// 打印登录成功信息到控制台,用于调试
|
|
|
System.out.println("登陆成功!");
|
|
|
// 设置请求属性,指定跳转的URL路径
|
|
|
req.setAttribute("httpUrl","/view/schoadmin/schomainview.jsp");
|
|
|
// 设置请求属性,指定提示信息
|
|
|
req.setAttribute("info", "登录成功!即将跳转至学校管理员后台首页!");
|
|
|
// 设置请求属性,指定页面标题
|
|
|
req.setAttribute("title","登录成功");
|
|
|
// 转发请求到指定的页面(/view/frontweb/info.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
|
|
|
|
|
|
}else {
|
|
|
// 如果结果集没有下一条记录,说明账号或密码错误
|
|
|
System.out.println("用户名或密码错误!请重新登录!");
|
|
|
// 设置请求属性,指定跳转回登录页面的URL路径
|
|
|
req.setAttribute("httpUrl","/view/frontweb/schoadmlogin.jsp");
|
|
|
// 设置请求属性,指定错误提示信息
|
|
|
req.setAttribute("info", "登录失败!管理员账号或密码错误!即将跳转至登录页面!");
|
|
|
// 设置请求属性,指定页面标题
|
|
|
req.setAttribute("title","登录失败");
|
|
|
// 转发请求到指定的页面(/view/frontweb/info.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 捕获异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库相关操作,如更新操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常类,用于处理Servlet在运行过程中可能出现的异常情况
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入注解,用于将Servlet映射到特定的URL
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,这是编写Servlet时继承的基础类,用于处理HTTP请求和响应
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类,通过它可以获取请求参数、请求头信息等
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类,通过它可以设置响应内容、状态码等
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关的信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,例如在读写数据时可能出现的异常
|
|
|
|
|
|
@WebServlet("/StuAlterPswServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuAlterPswServlet"
|
|
|
public class StuAlterPswServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"sno"的属性值,并将其转换为String类型,该值表示学生编号
|
|
|
String sno = (String) session.getAttribute("sno");
|
|
|
|
|
|
// 从请求参数中获取名为"spsw"的参数值,该值表示学生输入的新密码
|
|
|
String spsw = req.getParameter("spsw");
|
|
|
// 从请求参数中获取名为"spsw1"的参数值,该值表示学生再次输入的新密码用于确认
|
|
|
String spsw1 = req.getParameter("spsw1");
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (spsw.equals(spsw1)){
|
|
|
// 如果相同,则进行密码修改操作
|
|
|
// 构造SQL更新语句,用于更新student表中指定sno的学生的密码
|
|
|
String sql = "update student set spsw = ? where sno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是学生编号
|
|
|
Object[] objects = {spsw, sno};
|
|
|
// 调用FrontWebDao的executeUpdate方法执行SQL更新语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
if (num > 0){
|
|
|
// 如果受影响的行数大于0,说明密码修改成功
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示修改密码成功,并要求学生重新登录
|
|
|
// 同时将页面重定向到学生登录页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请重新登录!'); window.location='" + req.getContextPath() + "/view/frontweb/stulogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码修改失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示修改密码失败,并让学生重新输入密码
|
|
|
// 同时将页面重定向到学生修改密码页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/stualterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果两次输入的密码不同
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示两次密码不一样,并让学生重新输入密码
|
|
|
// 同时将页面重定向到学生修改密码页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/stualterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库操作,如更新密码的SQL语句
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常类,用于处理Servlet在运行过程中可能抛出的异常
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入注解,用于将Servlet映射到特定的URL
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,这是编写Servlet时的基类,用于处理HTTP请求和响应
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类,可获取请求参数等信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类,可设置响应内容、状态码等
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息(虽然此处未使用会话存储相关信息,但导入了该类)
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,例如在读写数据时可能出现的异常
|
|
|
|
|
|
@WebServlet("/StuForgetPswServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuForgetPswServlet"
|
|
|
public class StuForgetPswServlet 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");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"sno"的参数值,该值代表学生编号
|
|
|
String sno = req.getParameter("sno");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"spsw"的参数值,该值代表学生输入的新密码
|
|
|
String spsw = req.getParameter("spsw");
|
|
|
// 从HTTP请求的参数中获取名为"spsw1"的参数值,该值代表学生再次输入的用于确认的新密码
|
|
|
String spsw1 = req.getParameter("spsw1");
|
|
|
|
|
|
// 判断用户输入的新密码和确认密码是否相等
|
|
|
if (spsw.equals(spsw1)){
|
|
|
// 如果相等,则进行密码修改操作
|
|
|
// 构造SQL更新语句,用于更新student表中指定学生编号(sno)对应的密码(spsw)
|
|
|
String sql = "update student set spsw = ? where sno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是学生编号
|
|
|
Object[] objects = {spsw, sno};
|
|
|
// 调用FrontWebDao类中的executeUpdate方法执行SQL更新语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
if (num > 0){
|
|
|
// 如果受影响的行数大于0,说明密码更新操作成功
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码成功!请登录!”
|
|
|
// 并将页面重定向到学生登录页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/stulogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码更新操作失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码失败!请重新输入密码!”
|
|
|
// 并将页面重定向到学生忘记密码重置页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/stuforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果用户输入的新密码和确认密码不相等
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“两次密码不一样!请重新输入密码!”
|
|
|
// 并将页面重定向到学生忘记密码重置页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/stuforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库查询操作
|
|
|
|
|
|
import com.dao.StuDao;
|
|
|
// 导入StuDao类,这里未使用到,属于无用导入(可删除)
|
|
|
|
|
|
import com.entity.Student;
|
|
|
// 导入Student实体类,用于封装学生相关信息
|
|
|
|
|
|
import com.utils.JDBCUtils;
|
|
|
// 导入JDBC工具类,用于数据库连接的管理和资源关闭等操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读取或写入数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类,用于获取查询到的数据
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
// 导入ArrayList类,这里未使用到,属于无用导入(可删除)
|
|
|
|
|
|
@WebServlet("/StuInfoServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuInfoServlet"
|
|
|
public class StuInfoServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"sno"的属性值,并将其转换为String类型,该值表示学生编号
|
|
|
String sno = (String) session.getAttribute("sno");
|
|
|
|
|
|
// 构造SQL查询语句,用于从student表中查询指定学生编号(sno)的学生信息
|
|
|
String sql = "select * from student where sno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,参数为学生编号
|
|
|
Object[] objects = {sno};
|
|
|
// 调用FrontWebDao的qureyInfo方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = FrontWebDao.qureyInfo(sql, objects);
|
|
|
// 创建一个Student对象,用于存储查询到的学生信息
|
|
|
Student student = new Student();
|
|
|
|
|
|
try {
|
|
|
// 遍历结果集,将每一行数据封装到Student对象中
|
|
|
while (resultSet.next()){
|
|
|
// 设置学生编号
|
|
|
student.setSno(resultSet.getString("sno"));
|
|
|
// 设置学生姓名
|
|
|
student.setSname(resultSet.getString("sname"));
|
|
|
// 设置学生性别
|
|
|
student.setSsex(resultSet.getString("ssex"));
|
|
|
// 设置学生年龄,将数据库中的整数类型值转换为int类型
|
|
|
student.setSage(resultSet.getInt("sage"));
|
|
|
// 设置学生班级
|
|
|
student.setSclass(resultSet.getString("sclass"));
|
|
|
// 设置学生专业
|
|
|
student.setSpecialty(resultSet.getString("specialty"));
|
|
|
// 设置学生所在系部
|
|
|
student.setSdept(resultSet.getString("sdept"));
|
|
|
// 设置学生电话号码
|
|
|
student.setSphone(resultSet.getString("sphone"));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 捕获异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 无论是否发生异常,都调用JDBCUtils的close方法关闭结果集,释放资源
|
|
|
JDBCUtils.close(resultSet);
|
|
|
}
|
|
|
|
|
|
// 将封装好的学生信息对象(student)设置为请求属性,以便在后续页面中使用
|
|
|
req.setAttribute("student", student);
|
|
|
|
|
|
// 转发请求到指定的页面(/view/frontweb/stuinfo.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/stuinfo.jsp").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库查询操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读写数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
// 导入用于处理SQL异常的类
|
|
|
|
|
|
@WebServlet("/StuLoginServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuLoginServlet"
|
|
|
public class StuLoginServlet 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");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"sno"的参数值,该值代表学生编号
|
|
|
String sno = req.getParameter("sno");
|
|
|
// 从HTTP请求的参数中获取名为"spsw"的参数值,该值代表学生密码
|
|
|
String spsw = req.getParameter("spsw");
|
|
|
// 将获取到的学生编号打印到控制台,用于调试
|
|
|
System.out.println(sno);
|
|
|
// 将获取到的学生密码打印到控制台,用于调试
|
|
|
System.out.println(spsw);
|
|
|
|
|
|
// 获取当前请求的会话对象,如果不存在则创建一个新的会话
|
|
|
HttpSession session = req.getSession();
|
|
|
// 将学生编号存储到会话中,以便在后续请求中可以获取该信息
|
|
|
session.setAttribute("sno", sno);
|
|
|
|
|
|
// 构造SQL查询语句,用于从student表中查询匹配的学生记录
|
|
|
String sql = "select * from student where sno = ? and spsw = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为学生编号、学生密码
|
|
|
Object[] objects = {sno, spsw};
|
|
|
// 调用FrontWebDao的login方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = FrontWebDao.login(sql, objects);
|
|
|
|
|
|
try {
|
|
|
// 如果结果集有下一条记录,说明查询到了匹配的学生记录
|
|
|
if (resultSet.next()){
|
|
|
// 设置请求属性,指定跳转的URL路径为"/StuInfoServlet",即学生信息Servlet
|
|
|
req.setAttribute("httpUrl","/StuInfoServlet");
|
|
|
// 设置请求属性,指定提示信息
|
|
|
req.setAttribute("info", "登录成功!即将跳转至学生信息页面!");
|
|
|
// 设置请求属性,指定页面标题
|
|
|
req.setAttribute("title","登录成功");
|
|
|
// 转发请求到指定的页面(/view/frontweb/info.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
|
|
|
}else {
|
|
|
// 如果结果集没有下一条记录,说明学号或密码错误
|
|
|
// 设置请求属性,指定跳转回登录页面的URL路径
|
|
|
req.setAttribute("httpUrl","/view/frontweb/stulogin.jsp");
|
|
|
// 设置请求属性,指定错误提示信息
|
|
|
req.setAttribute("info", "登录失败!学号或密码错误!即将跳转至登录页面!");
|
|
|
// 设置请求属性,指定页面标题
|
|
|
req.setAttribute("title","登录失败");
|
|
|
// 转发请求到指定的页面(/view/frontweb/info.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
// 捕获SQL异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包为com.controller.frontweb
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入部门管理员数据访问对象类,但在本Servlet中未使用
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入前端数据访问对象类,用于执行数据库操作
|
|
|
|
|
|
import com.mysql.fabric.Response;
|
|
|
// 导入MySQL Fabric响应类,但在本Servlet中未使用
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import javax.swing.*;
|
|
|
// 导入Swing组件类,但在本Servlet中未使用
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读写数据时可能发生的异常
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
// 导入用于输出响应内容的类,但在本Servlet中未直接使用
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
// 导入用于处理SQL异常的类
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
// 导入用于格式化日期的类
|
|
|
|
|
|
import java.util.Date;
|
|
|
// 导入日期类,用于获取当前日期和时间
|
|
|
|
|
|
@WebServlet("/StuPunchServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuPunchServlet"
|
|
|
public class StuPunchServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"sno"的属性值,并将其转换为String类型,该值表示学生编号
|
|
|
String sno = (String) session.getAttribute("sno");
|
|
|
// 打印学生编号到控制台,用于调试
|
|
|
System.out.println(sno);
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"sishot"的参数值,该值表示学生是否发热
|
|
|
String sishot = req.getParameter("sishot");
|
|
|
// 从HTTP请求的参数中获取名为"siscough"的参数值,该值表示学生是否咳嗽
|
|
|
String siscough = req.getParameter("siscough");
|
|
|
// 从HTTP请求的参数中获取名为"sisseem"的参数值,该值表示学生是否有其他症状
|
|
|
String sisseem = req.getParameter("sisseem");
|
|
|
// 从HTTP请求的参数中获取名为"sisdiagnose"的参数值,该值表示学生是否被诊断
|
|
|
String sisdiagnose = req.getParameter("sisdiagnose");
|
|
|
// 从HTTP请求的参数中获取名为"sstatus"的参数值,该值表示学生当前状态
|
|
|
String sstatus = req.getParameter("sstatus");
|
|
|
|
|
|
// 设置学生打卡状态为"是",表示已打卡
|
|
|
String sispunch = "是";
|
|
|
// 创建Date对象,获取当前时间
|
|
|
Date date = new Date();
|
|
|
|
|
|
// 创建SimpleDateFormat对象,用于将日期格式化为"yyyy-MM-dd"的字符串
|
|
|
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// 格式化当前日期
|
|
|
String spunchdate = sf1.format(date);
|
|
|
// 打印当前日期到控制台,用于调试
|
|
|
System.out.println(spunchdate);
|
|
|
|
|
|
// 创建SimpleDateFormat对象,用于将时间格式化为"HH:mm"的字符串
|
|
|
SimpleDateFormat sf2 = new SimpleDateFormat("HH:mm");
|
|
|
// 格式化当前时间
|
|
|
String spunchtime = sf2.format(date);
|
|
|
// 打印当前时间到控制台,用于调试
|
|
|
System.out.println(spunchtime);
|
|
|
|
|
|
// 声明SQL语句变量,初始化为null
|
|
|
String sql = null;
|
|
|
|
|
|
// 构造SQL查询语句,用于查询该学生今天是否已经打卡
|
|
|
sql = "select count(*) as num from stupunchin where sno = ? and spunchdate = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为学生编号、打卡日期
|
|
|
Object[] objects1 = {sno, spunchdate};
|
|
|
// 调用FrontWebDao的findTotalCount方法执行SQL查询,获取查询结果的数量
|
|
|
int count = FrontWebDao.findTotalCount(sql, objects1);
|
|
|
|
|
|
if (count == 0){
|
|
|
// 如果查询结果数量为0,表示该学生今天还未打卡,则执行插入操作
|
|
|
sql = "insert into stupunchin values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为学生编号、是否打卡、打卡日期、打卡时间、是否发热、是否咳嗽、是否有其他症状、是否被诊断、当前状态
|
|
|
Object[] objects2 = {sno, sispunch, spunchdate, spunchtime, sishot, siscough, sisseem, sisdiagnose, sstatus};
|
|
|
|
|
|
// 调用FrontWebDao的executeUpdate方法执行SQL插入语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects2);
|
|
|
|
|
|
// 打印受影响的行数到控制台,用于调试
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示打卡成功
|
|
|
// 并将页面重定向到学生信息页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('打卡成功!'); window.location='" + req.getContextPath() + "/StuInfoServlet';" + "window.close();</script>");
|
|
|
|
|
|
}else {
|
|
|
// 如果查询结果数量不为0,表示该学生今天已经打卡
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示今天已打卡,不能再次打卡
|
|
|
// 并将页面重定向到学生信息页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('您今天已成功打卡!不能再次打卡!'); window.location='" + req.getContextPath() + "/StuInfoServlet';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库相关操作,如更新操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常类,用于处理Servlet运行时可能出现的异常
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入注解,用于将Servlet映射到特定的URL
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,这是编写Servlet时继承的基础类,用于处理HTTP请求和响应
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类,通过它可以获取请求参数、请求头信息等
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类,通过它可以设置响应内容、状态码等
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,例如在读写数据时可能出现的异常
|
|
|
|
|
|
@WebServlet("/TeaAlterPswServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/TeaAlterPswServlet"
|
|
|
public class TeaAlterPswServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"tno"的属性值,并将其转换为String类型,该值表示教师编号
|
|
|
String tno = (String) session.getAttribute("tno");
|
|
|
|
|
|
// 从请求参数中获取名为"tpsw"的参数值,该值表示教师输入的新密码
|
|
|
String tpsw = req.getParameter("tpsw");
|
|
|
// 从请求参数中获取名为"tpsw1"的参数值,该值表示教师再次输入的新密码用于确认
|
|
|
String tpsw1 = req.getParameter("tpsw1");
|
|
|
|
|
|
// 判断两次输入的密码是否相同
|
|
|
if (tpsw.equals(tpsw1)){
|
|
|
// 如果相同,则进行密码修改操作
|
|
|
// 构造SQL更新语句,用于更新teacher表中指定tno的教师的密码
|
|
|
String sql = "update teacher set tpsw = ? where tno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是教师编号
|
|
|
Object[] objects = {tpsw, tno};
|
|
|
// 调用FrontWebDao的executeUpdate方法执行SQL更新语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
if (num > 0){
|
|
|
// 如果受影响的行数大于0,说明密码修改成功
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示修改密码成功,并要求教师重新登录
|
|
|
// 同时将页面重定向到教师登录页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请重新登录!'); window.location='" + req.getContextPath() + "/view/frontweb/tealogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码修改失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示修改密码失败,并让教师重新输入密码
|
|
|
// 同时将页面重定向到教师修改密码页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/teaalterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果两次输入的密码不同
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示两次密码不一样,并让教师重新输入密码
|
|
|
// 同时将页面重定向到教师修改密码页面,并关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/teaalterpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库操作,如更新密码相关的SQL操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常类,用于处理Servlet在运行过程中可能出现的异常情况
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入注解,用于将Servlet映射到特定的URL
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,所有基于HTTP协议的Servlet都继承自该类,用于处理HTTP请求和响应
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类,通过它可以获取请求参数等信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类,通过它可以设置响应内容、状态码等
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,例如在读写数据时可能出现的异常
|
|
|
|
|
|
@WebServlet("/TeaForgetPswServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/TeaForgetPswServlet"
|
|
|
public class TeaForgetPswServlet 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");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"tno"的参数值,该值代表教师编号
|
|
|
String tno = req.getParameter("tno");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"tpsw"的参数值,该值代表教师输入的新密码
|
|
|
String tpsw = req.getParameter("tpsw");
|
|
|
// 从HTTP请求的参数中获取名为"tpsw1"的参数值,该值代表教师再次输入的用于确认的新密码
|
|
|
String tpsw1 = req.getParameter("tpsw1");
|
|
|
|
|
|
// 判断教师输入的新密码和确认密码是否相等
|
|
|
if (tpsw.equals(tpsw1)){
|
|
|
// 如果相等,则进行密码修改操作
|
|
|
// 构造SQL更新语句,用于更新teacher表中指定教师编号(tno)对应的密码(tpsw)
|
|
|
String sql = "update teacher set tpsw = ? where tno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组,第一个参数是新密码,第二个参数是教师编号
|
|
|
Object[] objects = {tpsw, tno};
|
|
|
// 调用FrontWebDao类中的executeUpdate方法执行SQL更新语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects);
|
|
|
if (num > 0){
|
|
|
// 如果受影响的行数大于0,说明密码更新操作成功
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码成功!请登录!”
|
|
|
// 并将页面重定向到教师登录页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码成功!请登录!'); window.location='" + req.getContextPath() + "/view/frontweb/tealogin.jsp';" + "window.close();</script>");
|
|
|
}else {
|
|
|
// 如果受影响的行数不大于0,说明密码更新操作失败
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“修改密码失败!请重新输入密码!”
|
|
|
// 并将页面重定向到教师忘记密码重置页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('修改密码失败!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/teaforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}else {
|
|
|
// 如果教师输入的新密码和确认密码不相等
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框显示“两次密码不一样!请重新输入密码!”
|
|
|
// 并将页面重定向到教师忘记密码重置页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('两次密码不一样!请重新输入密码!'); window.location='" + req.getContextPath() + "/view/frontweb/teaforgetpsw.jsp';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库查询操作
|
|
|
|
|
|
import com.entity.Student;
|
|
|
// 导入Student实体类,这里未使用到,属于无用导入(可删除)
|
|
|
|
|
|
import com.entity.Teacher;
|
|
|
// 导入Teacher实体类,用于封装教师相关信息
|
|
|
|
|
|
import com.utils.JDBCUtils;
|
|
|
// 导入JDBC工具类,用于数据库连接的管理和资源关闭等操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读取或写入数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类,用于获取查询到的数据
|
|
|
|
|
|
@WebServlet("/TeaInfoServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/TeaInfoServlet"
|
|
|
public class TeaInfoServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"tno"的属性值,并将其转换为String类型,该值表示教师编号
|
|
|
String tno = (String) session.getAttribute("tno");
|
|
|
|
|
|
// 构造SQL查询语句,用于从teacher表中查询指定教师编号(tno)的教师信息
|
|
|
String sql = "select * from teacher where tno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,参数为教师编号
|
|
|
Object[] objects = {tno};
|
|
|
// 调用FrontWebDao的qureyInfo方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = FrontWebDao.qureyInfo(sql, objects);
|
|
|
// 创建一个Teacher对象,用于存储查询到的教师信息
|
|
|
Teacher teacher = new Teacher();
|
|
|
|
|
|
try {
|
|
|
// 遍历结果集,将每一行数据封装到Teacher对象中
|
|
|
while (resultSet.next()){
|
|
|
// 设置教师编号
|
|
|
teacher.setTno(resultSet.getString("tno"));
|
|
|
// 设置教师姓名
|
|
|
teacher.setTname(resultSet.getString("tname"));
|
|
|
// 设置教师性别
|
|
|
teacher.setTsex(resultSet.getString("tsex"));
|
|
|
// 设置教师年龄,将数据库中的整数类型值转换为int类型
|
|
|
teacher.setTage(resultSet.getInt("tage"));
|
|
|
// 设置教师所在系部
|
|
|
teacher.setTdept(resultSet.getString("tdept"));
|
|
|
// 设置教师电话号码
|
|
|
teacher.setTphone(resultSet.getString("tphone"));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 捕获异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 无论是否发生异常,都调用JDBCUtils的close方法关闭结果集,释放资源
|
|
|
JDBCUtils.close(resultSet);
|
|
|
}
|
|
|
|
|
|
// 将封装好的教师信息对象(teacher)设置为请求属性,以便在后续页面中使用
|
|
|
req.setAttribute("teacher", teacher);
|
|
|
|
|
|
// 转发请求到指定的页面(/view/frontweb/teainfo.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/teainfo.jsp").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库查询操作
|
|
|
|
|
|
import com.entity.Student;
|
|
|
// 导入Student实体类,这里未使用到,属于无用导入(可删除)
|
|
|
|
|
|
import com.entity.Teacher;
|
|
|
// 导入Teacher实体类,用于封装教师相关信息
|
|
|
|
|
|
import com.utils.JDBCUtils;
|
|
|
// 导入JDBC工具类,用于数据库连接的管理和资源关闭等操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读取或写入数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类,用于获取查询到的数据
|
|
|
|
|
|
@WebServlet("/TeaInfoServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/TeaInfoServlet"
|
|
|
public class TeaInfoServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"tno"的属性值,并将其转换为String类型,该值表示教师编号
|
|
|
String tno = (String) session.getAttribute("tno");
|
|
|
|
|
|
// 构造SQL查询语句,用于从teacher表中查询指定教师编号(tno)的教师信息
|
|
|
String sql = "select * from teacher where tno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,参数为教师编号
|
|
|
Object[] objects = {tno};
|
|
|
// 调用FrontWebDao的qureyInfo方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = FrontWebDao.qureyInfo(sql, objects);
|
|
|
// 创建一个Teacher对象,用于存储查询到的教师信息
|
|
|
Teacher teacher = new Teacher();
|
|
|
|
|
|
try {
|
|
|
// 遍历结果集,将每一行数据封装到Teacher对象中
|
|
|
while (resultSet.next()){
|
|
|
// 设置教师编号
|
|
|
teacher.setTno(resultSet.getString("tno"));
|
|
|
// 设置教师姓名
|
|
|
teacher.setTname(resultSet.getString("tname"));
|
|
|
// 设置教师性别
|
|
|
teacher.setTsex(resultSet.getString("tsex"));
|
|
|
// 设置教师年龄,将数据库中的整数类型值转换为int类型
|
|
|
teacher.setTage(resultSet.getInt("tage"));
|
|
|
// 设置教师所在系部
|
|
|
teacher.setTdept(resultSet.getString("tdept"));
|
|
|
// 设置教师电话号码
|
|
|
teacher.setTphone(resultSet.getString("tphone"));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 捕获异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 无论是否发生异常,都调用JDBCUtils的close方法关闭结果集,释放资源
|
|
|
JDBCUtils.close(resultSet);
|
|
|
}
|
|
|
|
|
|
// 将封装好的教师信息对象(teacher)设置为请求属性,以便在后续页面中使用
|
|
|
req.setAttribute("teacher", teacher);
|
|
|
|
|
|
// 转发请求到指定的页面(/view/frontweb/teainfo.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/teainfo.jsp").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入数据访问对象类FrontWebDao,用于执行数据库查询操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读写数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
// 导入用于处理SQL异常的类
|
|
|
|
|
|
@WebServlet("/TeaLoginServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/TeaLoginServlet"
|
|
|
public class TeaLoginServlet 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");
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"tno"的参数值,该值代表教师编号
|
|
|
String tno = req.getParameter("tno");
|
|
|
// 从HTTP请求的参数中获取名为"tpsw"的参数值,该值代表教师密码
|
|
|
String tpsw = req.getParameter("tpsw");
|
|
|
// 将获取到的教师编号打印到控制台,用于调试
|
|
|
System.out.println(tno);
|
|
|
// 将获取到的教师密码打印到控制台,用于调试
|
|
|
System.out.println(tpsw);
|
|
|
|
|
|
// 获取当前请求的会话对象,如果不存在则创建一个新的会话
|
|
|
HttpSession session = req.getSession();
|
|
|
// 将教师编号存储到会话中,以便在后续请求中可以获取该信息
|
|
|
session.setAttribute("tno", tno);
|
|
|
|
|
|
// 构造SQL查询语句,用于从teacher表中查询匹配的教师记录
|
|
|
String sql = "select * from teacher where tno = ? and tpsw = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为教师编号、教师密码
|
|
|
Object[] objects = {tno, tpsw};
|
|
|
// 调用FrontWebDao的login方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = FrontWebDao.login(sql, objects);
|
|
|
|
|
|
try {
|
|
|
// 如果结果集有下一条记录,说明查询到了匹配的教师记录
|
|
|
if (resultSet.next()){
|
|
|
// 设置请求属性,指定跳转的URL路径为"/TeaInfoServlet",即教师信息Servlet
|
|
|
req.setAttribute("httpUrl","/TeaInfoServlet");
|
|
|
// 设置请求属性,指定提示信息
|
|
|
req.setAttribute("info", "登录成功!即将跳转至教师信息页面!");
|
|
|
// 设置请求属性,指定页面标题
|
|
|
req.setAttribute("title","登录成功");
|
|
|
// 转发请求到指定的页面(/view/frontweb/info.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
|
|
|
}else {
|
|
|
// 如果结果集没有下一条记录,说明教工号或密码错误
|
|
|
// 设置请求属性,指定跳转回登录页面的URL路径
|
|
|
req.setAttribute("httpUrl","/view/frontweb/tealogin.jsp");
|
|
|
// 设置请求属性,指定错误提示信息
|
|
|
req.setAttribute("info", "登录失败!教工号或密码错误!即将跳转至登录页面!");
|
|
|
// 设置请求属性,指定页面标题
|
|
|
req.setAttribute("title","登录失败");
|
|
|
// 转发请求到指定的页面(/view/frontweb/info.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/frontweb/info.jsp").forward(req, resp);
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
// 捕获SQL异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.frontweb;
|
|
|
// 声明该Servlet所在的包名为com.controller.frontweb
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入部门管理员数据访问对象类,在本类中未使用
|
|
|
|
|
|
import com.dao.FrontWebDao;
|
|
|
// 导入前端数据访问对象类,用于执行数据库操作,如查询、插入等
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读写数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
// 导入用于格式化日期和时间的类
|
|
|
|
|
|
import java.util.Date;
|
|
|
// 导入日期类,用于获取当前日期和时间
|
|
|
|
|
|
@WebServlet("/TeaPunchServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/TeaPunchServlet"
|
|
|
public class TeaPunchServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"tno"的属性值,并将其转换为String类型,该值表示教师编号
|
|
|
String tno = (String) session.getAttribute("tno");
|
|
|
// 将教师编号打印到控制台,用于调试
|
|
|
System.out.println(tno);
|
|
|
|
|
|
// 从HTTP请求的参数中获取名为"tishot"的参数值,该值表示教师是否发热
|
|
|
String tishot = req.getParameter("tishot");
|
|
|
// 从HTTP请求的参数中获取名为"tiscough"的参数值,该值表示教师是否咳嗽
|
|
|
String tiscough = req.getParameter("tiscough");
|
|
|
// 从HTTP请求的参数中获取名为"tisseem"的参数值,该值表示教师是否有其他症状
|
|
|
String tisseem = req.getParameter("tisseem");
|
|
|
// 从HTTP请求的参数中获取名为"tisdiagnose"的参数值,该值表示教师是否被诊断
|
|
|
String tisdiagnose = req.getParameter("tisdiagnose");
|
|
|
// 从HTTP请求的参数中获取名为"tstatus"的参数值,该值表示教师当前状态
|
|
|
String tstatus = req.getParameter("tstatus");
|
|
|
|
|
|
// 设置教师打卡状态为"是",表示已打卡
|
|
|
String tispunch = "是";
|
|
|
// 创建Date对象,获取当前时间
|
|
|
Date date = new Date();
|
|
|
|
|
|
// 创建SimpleDateFormat对象,用于将日期格式化为"yyyy-MM-dd"的字符串
|
|
|
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// 格式化当前日期
|
|
|
String tpunchdate = sf1.format(date);
|
|
|
// 将当前日期打印到控制台,用于调试
|
|
|
System.out.println(tpunchdate);
|
|
|
|
|
|
// 创建SimpleDateFormat对象,用于将时间格式化为"HH:mm"的字符串
|
|
|
SimpleDateFormat sf2 = new SimpleDateFormat("HH:mm");
|
|
|
// 格式化当前时间
|
|
|
String tpunchtime = sf2.format(date);
|
|
|
// 将当前时间打印到控制台,用于调试
|
|
|
System.out.println(tpunchtime);
|
|
|
|
|
|
// 初始化SQL语句变量
|
|
|
String sql = null;
|
|
|
|
|
|
// 构造SQL查询语句,用于查询该教师今天是否已经打卡
|
|
|
sql = "select count(*) as num from teapunchin where tno = ? and tpunchdate = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为教师编号、打卡日期
|
|
|
Object[] objects1 = {tno, tpunchdate};
|
|
|
// 调用FrontWebDao的findTotalCount方法执行SQL查询,获取查询结果的数量
|
|
|
int count = FrontWebDao.findTotalCount(sql, objects1);
|
|
|
|
|
|
if (count == 0) {
|
|
|
// 如果查询结果数量为0,表示该教师今天还未打卡,则执行插入操作
|
|
|
sql = "insert into teapunchin values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
// 创建包含SQL语句参数的对象数组,依次为教师编号、是否打卡、打卡日期、打卡时间、是否发热、是否咳嗽、是否有其他症状、是否被诊断、当前状态
|
|
|
Object[] objects2 = {tno, tispunch, tpunchdate, tpunchtime, tishot, tiscough, tisseem, tisdiagnose, tstatus};
|
|
|
|
|
|
// 调用FrontWebDao的executeUpdate方法执行SQL插入语句,并获取受影响的行数
|
|
|
int num = FrontWebDao.executeUpdate(sql, objects2);
|
|
|
|
|
|
// 将受影响的行数打印到控制台,用于调试
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示打卡成功
|
|
|
// 并将页面重定向到教师信息页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('打卡成功!'); window.location='" + req.getContextPath() + "/TeaInfoServlet';" + "window.close();</script>");
|
|
|
|
|
|
} else {
|
|
|
// 如果查询结果数量不为0,表示该教师今天已经打卡
|
|
|
// 通过响应输出一段JavaScript代码,弹出提示框提示今天已打卡,不能再次打卡
|
|
|
// 并将页面重定向到教师信息页面,同时关闭当前窗口
|
|
|
resp.getWriter().write("<script>alert('您今天已成功打卡!不能再次打卡!'); window.location='" + req.getContextPath() + "/TeaInfoServlet';" + "window.close();</script>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
this.doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入自定义的DeptAdminDao类,用于数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入ServletException类,用于处理Servlet异常
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入WebServlet注解,用于将Servlet映射到URL
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet类,所有Servlet都继承自该类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入HttpServletRequest类,用于获取客户端请求信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入HttpServletResponse类,用于向客户端发送响应信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入IOException类,用于处理输入输出异常
|
|
|
|
|
|
/**
|
|
|
* 学校管理员添加学生打卡记录的Servlet
|
|
|
* 处理添加学生打卡信息的请求,检查记录是否存在并执行相应操作
|
|
|
*/
|
|
|
@WebServlet("/SchoAddStuPunchServlet")
|
|
|
public class SchoAddStuPunchServlet extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求
|
|
|
* @param req HttpServletRequest对象,包含客户端请求信息
|
|
|
* @param resp HttpServletResponse对象,用于向客户端发送响应
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求编码为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 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"); // 健康状态
|
|
|
|
|
|
// 声明SQL语句变量
|
|
|
String sql = null;
|
|
|
|
|
|
// 打印调试信息
|
|
|
System.out.println("shgshgh");
|
|
|
|
|
|
// 查询是否存在该学生在指定日期的打卡记录
|
|
|
sql = "select count(*) as num from stupunchin where sno = ? and spunchdate = ?";
|
|
|
Object[] objects = {sno, spunchdate};
|
|
|
// 调用DeptAdminDao的方法查询记录数量
|
|
|
int count = DeptAdminDao.findTotalCount(sql, objects);
|
|
|
|
|
|
// 如果记录不存在
|
|
|
if (count == 0) {
|
|
|
// 插入新的打卡记录
|
|
|
sql = "insert into stupunchin values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
Object[] objects1 = {sno, sispunch, spunchdate, spunchtime, sishot, siscough, sisseem, sisdiagnose, sstatus};
|
|
|
|
|
|
// 执行插入操作并获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects1);
|
|
|
|
|
|
// 打印插入结果
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 重定向到分页查询Servlet,显示第一页数据
|
|
|
req.getRequestDispatcher("/SchoQueryStuPunchByPageServlet?currentPage=1&rows=7&sname=&sclass=&sdept=&spunchdate=").forward(req, resp);
|
|
|
} else {
|
|
|
// 如果记录已存在,转发到提示页面
|
|
|
req.getRequestDispatcher("/view/alluse/existdataofadd.jsp").forward(req, resp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求,直接调用doGet方法处理
|
|
|
* @param req HttpServletRequest对象,包含客户端请求信息
|
|
|
* @param resp HttpServletResponse对象,用于向客户端发送响应
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入自定义的数据访问对象类,用于数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常处理类
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入Servlet注解,用于映射Servlet到URL路径
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet基类,所有Servlet都继承自此类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入HTTP请求对象类,用于获取客户端请求信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入HTTP响应对象类,用于向客户端发送响应
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入输入输出异常类
|
|
|
|
|
|
/**
|
|
|
* 学校管理员添加学生信息的Servlet
|
|
|
* 处理添加学生信息的请求,检查学生是否已存在并执行相应操作
|
|
|
*/
|
|
|
@WebServlet("/SchoAddStuServlet")
|
|
|
public class SchoAddStuServlet extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求的方法
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求编码为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 specialty = req.getParameter("specialty"); // 专业
|
|
|
String sclass = req.getParameter("sclass"); // 班级
|
|
|
String sdept = req.getParameter("sdept"); // 系部
|
|
|
String sphone = req.getParameter("sphone"); // 联系电话
|
|
|
String spsw = req.getParameter("spsw"); // 密码
|
|
|
|
|
|
// 将年龄字符串转换为整数类型
|
|
|
int sage1 = Integer.parseInt(sage);
|
|
|
|
|
|
// 声明SQL语句变量
|
|
|
String sql = null;
|
|
|
|
|
|
// 打印调试信息
|
|
|
System.out.println("shgshgh");
|
|
|
|
|
|
// 查询数据库中是否已存在该学号的学生
|
|
|
sql = "select count(*) as num from student where sno = ?";
|
|
|
Object[] objects = {sno};
|
|
|
// 调用DAO方法执行查询,获取记录数量
|
|
|
int count = DeptAdminDao.findTotalCount(sql, objects);
|
|
|
|
|
|
// 如果学生不存在
|
|
|
if (count == 0) {
|
|
|
// 构造插入学生信息的SQL语句
|
|
|
sql = "insert into student values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
Object[] objects1 = {sno, sname, ssex, sage1, sclass, specialty, sdept, sphone, spsw};
|
|
|
|
|
|
// 调用DAO方法执行插入操作,获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects1);
|
|
|
|
|
|
// 打印插入结果
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 转发请求到分页查询Servlet,显示第一页学生列表
|
|
|
req.getRequestDispatcher("/SchoQueryStuByPageServlet?currentPage=1&rows=7&sname=&sclass=&sdept=").forward(req, resp);
|
|
|
} else {
|
|
|
// 如果学生已存在,转发到提示页面
|
|
|
req.getRequestDispatcher("/view/alluse/existdataofadd.jsp").forward(req, resp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求的方法,直接调用doGet方法处理
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入自定义的数据访问对象类,用于数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常处理类
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入Servlet注解,用于映射Servlet到URL路径
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet基类,所有Servlet都继承自此类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入HTTP请求对象类,用于获取客户端请求信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入HTTP响应对象类,用于向客户端发送响应
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入输入输出异常类
|
|
|
|
|
|
/**
|
|
|
* 学校管理员添加教师打卡记录的Servlet
|
|
|
* 处理添加教师打卡信息的请求,检查记录是否存在并执行相应操作
|
|
|
*/
|
|
|
@WebServlet("/SchoAddTeaPunchServlet")
|
|
|
public class SchoAddTeaPunchServlet extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求的方法
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求编码为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 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"); // 健康状态
|
|
|
|
|
|
// 声明SQL语句变量
|
|
|
String sql = null;
|
|
|
|
|
|
// 打印调试信息
|
|
|
System.out.println("shgshgh");
|
|
|
|
|
|
// 查询数据库中是否已存在该教师在指定日期的打卡记录
|
|
|
sql = "select count(*) as num from teapunchin where tno = ? and tpunchdate = ?";
|
|
|
Object[] objects = {tno, tpunchdate};
|
|
|
// 调用DAO方法执行查询,获取记录数量
|
|
|
int count = DeptAdminDao.findTotalCount(sql, objects);
|
|
|
|
|
|
// 如果记录不存在
|
|
|
if (count == 0) {
|
|
|
// 构造插入教师打卡记录的SQL语句
|
|
|
sql = "insert into teapunchin values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
|
Object[] objects1 = {tno, tispunch, tpunchdate, tpunchtime, tishot, tiscough, tisseem, tisdiagnose, tstatus};
|
|
|
|
|
|
// 调用DAO方法执行插入操作,获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects1);
|
|
|
|
|
|
// 打印插入结果
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 转发请求到分页查询Servlet,显示第一页教师打卡记录
|
|
|
req.getRequestDispatcher("/SchoQueryTeaPunchByPageServlet?currentPage=1&rows=7&tno=&tname=&tdept=&tpunchdate=").forward(req, resp);
|
|
|
} else {
|
|
|
// 如果记录已存在,转发到提示页面
|
|
|
req.getRequestDispatcher("/view/alluse/existdataofadd.jsp").forward(req, resp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求的方法,直接调用doGet方法处理
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入数据访问对象类,用于执行数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常处理类
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入Servlet注解,用于映射Servlet到URL路径
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet基类,所有Servlet都继承自此类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入HTTP请求对象类,用于获取客户端请求信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入HTTP响应对象类,用于向客户端发送响应
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入输入输出异常类
|
|
|
|
|
|
/**
|
|
|
* 学校管理员修改学生打卡记录的Servlet
|
|
|
* 处理修改学生打卡信息的请求,更新数据库中的记录
|
|
|
*/
|
|
|
@WebServlet("/SchoAlterStuPunchServlet")
|
|
|
public class SchoAlterStuPunchServlet extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求的方法
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求编码为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 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"); // 健康状态
|
|
|
|
|
|
// 构造更新学生打卡记录的SQL语句
|
|
|
String sql = "update stupunchin set sispunch = ?, spunchtime = ?, sishot = ?, siscough = ?, sisseem = ?, sisdiagnose = ?, sstatus = ? where sno = ? and spunchdate = ?";
|
|
|
// 准备SQL语句的参数
|
|
|
Object[] objects = {sispunch, spunchtime, sishot, siscough, sisseem, sisdiagnose, sstatus, sno, spunchdate};
|
|
|
|
|
|
// 调用DAO方法执行更新操作,获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 打印更新结果
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 转发请求到分页查询Servlet,显示第一页学生打卡记录
|
|
|
req.getRequestDispatcher("/SchoQueryStuPunchByPageServlet?currentPage=1&rows=7&sname=&sclass=&sdept=&spunchdate=").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求的方法,直接调用doGet方法处理
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入自定义的数据访问对象类,用于数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常处理类
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入Servlet注解,用于映射Servlet到URL路径
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet基类,所有Servlet都继承自此类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入HTTP请求对象类,用于获取客户端请求信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入HTTP响应对象类,用于向客户端发送响应
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入输入输出异常类
|
|
|
|
|
|
/**
|
|
|
* 学校管理员修改教师打卡记录的Servlet
|
|
|
* 处理修改教师打卡信息的请求,更新数据库中的相关记录
|
|
|
*/
|
|
|
@WebServlet("/SchoAlterTeaPunchServlet")
|
|
|
public class SchoAlterTeaPunchServlet extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求的方法
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求编码为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 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"); // 健康状态
|
|
|
|
|
|
// 构造用于更新教师打卡记录的SQL语句
|
|
|
String sql = "update teapunchin set tispunch = ?, tpunchtime = ?, tishot = ?, tiscough = ?, tisseem = ?, tisdiagnose = ?, tstatus = ? where tno = ? and tpunchdate = ?";
|
|
|
// 准备SQL语句的参数
|
|
|
Object[] objects = {tispunch, tpunchtime, tishot, tiscough, tisseem, tisdiagnose, tstatus, tno, tpunchdate};
|
|
|
|
|
|
// 调用DeptAdminDao的方法执行SQL更新操作,获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 打印更新操作影响的行数,用于调试
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 转发请求到分页查询教师打卡记录的Servlet,设置当前页为1,每页显示7条记录,其他查询参数为空
|
|
|
req.getRequestDispatcher("/SchoQueryTeaPunchByPageServlet?currentPage=1&rows=7&tno=&tname=&tdept=&tpunchdate=").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求的方法,直接调用doGet方法处理
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
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;
|
|
|
// 导入用于处理输入输出异常的类,如文件读写、网络传输等操作中可能出现的异常
|
|
|
|
|
|
@WebServlet("/SchoAlterTeaServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/SchoAlterTeaServlet"
|
|
|
public class SchoAlterTeaServlet 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 = "update teacher set tname = ?, tsex = ?, tage = ?, tdept = ?, tphone = ?, tpsw = ? where tno = ?";
|
|
|
// 创建一个包含SQL语句参数的对象数组
|
|
|
Object[] objects = {tname, tsex, tage1, tdept, tphone, tpsw, tno};
|
|
|
|
|
|
// 调用DeptAdminDao的executeUpdate方法执行SQL更新操作,并获取受影响的行数
|
|
|
int num = DeptAdminDao.executeUpdate(sql, objects);
|
|
|
|
|
|
// 打印受影响的行数,用于调试和查看更新操作是否成功
|
|
|
System.out.println(num);
|
|
|
|
|
|
// 转发请求到分页查询教师信息的Servlet,设置当前页为1,每页显示7条记录,其他查询条件为空
|
|
|
req.getRequestDispatcher("/SchoQueryTeaByPageServlet?currentPage=1&rows=7&tno=&tname=&tdept=").forward(req, resp);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 重写父类HttpServlet的doPost方法,用于处理HTTP POST请求
|
|
|
// 直接调用doGet方法来处理POST请求,复用doGet方法中的业务逻辑
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入数据访问对象类,用于执行数据库操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常处理类
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入Servlet注解,用于映射Servlet到URL路径
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet基类,所有Servlet都继承自此类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入HTTP请求对象类,用于获取客户端请求信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入HTTP响应对象类,用于向客户端发送响应
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入输入输出异常类
|
|
|
|
|
|
/**
|
|
|
* 学校管理员删除学生打卡记录的Servlet
|
|
|
* 处理删除学生打卡信息的请求,检查记录是否存在并执行删除操作
|
|
|
*/
|
|
|
@WebServlet("/SchoDeleteStuPunchServlet")
|
|
|
public class SchoDeleteStuPunchServlet extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求的方法
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求编码为UTF-8,确保中文参数能正确解析
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
// 设置响应编码为UTF-8
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
|
// 设置响应内容类型为HTML,编码为UTF-8
|
|
|
resp.setContentType("text/html;charset=utf-8");
|
|
|
|
|
|
// 获取请求参数,参数格式为"学号,打卡日期"
|
|
|
String snodate = req.getParameter("snodate");
|
|
|
|
|
|
// 打印获取的参数,用于调试
|
|
|
System.out.println(snodate);
|
|
|
|
|
|
// 分离学号和打卡日期两个参数,使用逗号作为分隔符
|
|
|
String[] params = snodate.split(",");
|
|
|
String sno = params[0]; // 学生学号
|
|
|
String spunchdate = params[1]; // 打卡日期
|
|
|
|
|
|
// 打印分离后的参数,用于调试
|
|
|
System.out.println(sno);
|
|
|
System.out.println(spunchdate);
|
|
|
|
|
|
// 注释掉的代码,尝试将字符串转换为日期类型(实际未使用)
|
|
|
// Date spunchdate1 = new Date(spunchdate);
|
|
|
// SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// Date spunchdate1 = ft.format(spunchdate);
|
|
|
|
|
|
// 声明SQL语句变量
|
|
|
String sql = null;
|
|
|
// 准备SQL语句的参数
|
|
|
Object[] objects = {sno, spunchdate};
|
|
|
|
|
|
// 查询是否存在该学生在指定日期的打卡记录
|
|
|
sql = "select count(*) as num from stupunchin where sno = ? and spunchdate = ?";
|
|
|
// 调用DAO方法执行查询,获取记录数量
|
|
|
int count = DeptAdminDao.findTotalCount(sql, objects);
|
|
|
|
|
|
// 如果记录存在
|
|
|
if (count > 0) {
|
|
|
// 构造删除学生打卡记录的SQL语句
|
|
|
sql = "delete from stupunchin where sno = ? and spunchdate = ?";
|
|
|
// 调用DAO方法执行删除操作,获取受影响的行数
|
|
|
int num1 = DeptAdminDao.executeUpdate(sql, objects);
|
|
|
// 打印删除结果
|
|
|
System.out.println(num1);
|
|
|
|
|
|
// 转发请求到分页查询Servlet,显示第一页学生打卡记录
|
|
|
req.getRequestDispatcher("/SchoQueryStuPunchByPageServlet?currentPage=1&rows=7&sname=&sclass=&sdept=&spunchdate=").forward(req, resp);
|
|
|
} else {
|
|
|
// 如果记录不存在,转发到提示页面
|
|
|
req.getRequestDispatcher("/view/alluse/noexistdataofdelete.jsp").forward(req, resp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求的方法,直接调用doGet方法处理
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.schoadmin;
|
|
|
// 声明该类所在的包名为com.controller.schoadmin
|
|
|
|
|
|
import com.dao.DeptAdminDao;
|
|
|
// 导入自定义的数据访问对象类,用于数据库的查询和更新操作
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
// 导入Servlet异常处理类,用于处理Servlet运行过程中可能出现的异常
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
// 导入Servlet注解,用于将Servlet映射到指定的URL路径
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
// 导入HttpServlet基类,所有基于HTTP协议的Servlet都继承自该类
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
// 导入用于处理HTTP请求的类,可获取请求参数、会话等信息
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
// 导入用于处理HTTP响应的类,可设置响应内容、状态码等
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
// 导入用于处理HTTP会话的类,可获取和设置会话属性
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如文件读写、网络传输等操作中可能出现的异常
|
|
|
|
|
|
@WebServlet("/SchoDeleteStuPunchServlet1")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/SchoDeleteStuPunchServlet1"
|
|
|
public class SchoDeleteStuPunchServlet1 extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理GET请求的方法
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置请求的字符编码为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 spunchdate = req.getParameter("spunchdate"); // 打卡日期
|
|
|
|
|
|
// 打印获取的学号参数,用于调试
|
|
|
System.out.println(sno);
|
|
|
// 打印获取的打卡日期参数,用于调试
|
|
|
System.out.println(spunchdate);
|
|
|
|
|
|
// 获取登录时的session会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// String userName = (String) session.getAttribute("userName");
|
|
|
// String sno = (String) session.getAttribute("sno");
|
|
|
String belong = (String) session.getAttribute("belong"); // 获取会话中存储的"belong"属性值
|
|
|
|
|
|
// 注释掉的代码,尝试将字符串转换为日期类型(实际未使用)
|
|
|
// Date spunchdate1 = new Date(spunchdate);
|
|
|
// SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// Date spunchdate1 = ft.format(spunchdate);
|
|
|
|
|
|
// 声明SQL语句变量
|
|
|
String sql = null;
|
|
|
// 创建包含SQL语句参数的对象数组
|
|
|
Object[] objects = {sno, spunchdate};
|
|
|
|
|
|
// 查询是否存在符合条件的学生打卡记录(通过关联student和stupunchin表)
|
|
|
sql = "select count(*) as num from student s,stupunchin sp where s.sno = sp.sno and s.sno = ? and sp.spunchdate = ?";
|
|
|
// 调用DeptAdminDao的方法执行SQL查询,获取符合条件的记录数量
|
|
|
int count = DeptAdminDao.findTotalCount(sql, objects);
|
|
|
|
|
|
// 如果存在符合条件的记录
|
|
|
if (count > 0) {
|
|
|
// 构造删除学生打卡记录的SQL语句
|
|
|
sql = "delete from stupunchin where sno = ? and spunchdate = ?";
|
|
|
// 调用DeptAdminDao的方法执行SQL删除操作,获取受影响的行数
|
|
|
int num1 = DeptAdminDao.executeUpdate(sql, objects);
|
|
|
// 打印受影响的行数,用于调试
|
|
|
System.out.println(num1);
|
|
|
|
|
|
// 转发请求到分页查询学生打卡记录的Servlet,传递参数设置当前页为1,每页显示7条记录,其他查询条件为空
|
|
|
req.getRequestDispatcher("/SchoQueryStuPunchByPageServlet?currentPage=1&rows=7&sname=&sclass=&sdept=&spunchdate=").forward(req, resp);
|
|
|
} else {
|
|
|
// 如果不存在符合条件的记录,转发请求到提示已存在数据的页面
|
|
|
req.getRequestDispatcher("/view/alluse/noexistdataofdelete.jsp").forward(req, resp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理POST请求的方法,直接调用doGet方法处理
|
|
|
* @param req HTTP请求对象
|
|
|
* @param resp HTTP响应对象
|
|
|
* @throws ServletException 如果处理请求时发生Servlet异常
|
|
|
* @throws IOException 如果发生输入输出异常
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.stu;
|
|
|
// 声明该Servlet所在的包名为com.controller.stu
|
|
|
|
|
|
import com.dao.StuDao;
|
|
|
// 导入学生数据访问对象类StuDao,用于执行数据库查询操作
|
|
|
|
|
|
import com.entity.Student;
|
|
|
// 导入学生实体类,用于封装学生相关信息
|
|
|
|
|
|
import com.utils.JDBCUtils;
|
|
|
// 导入JDBC工具类,用于数据库连接的管理和资源关闭等操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读取或写入数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类,用于获取查询到的数据
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
// 导入ArrayList类,用于存储多个学生对象
|
|
|
|
|
|
@WebServlet("/StuQueryInfoServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuQueryInfoServlet"
|
|
|
public class StuQueryInfoServlet 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");
|
|
|
|
|
|
// 获取当前请求的会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"userName"的属性值,并将其转换为String类型
|
|
|
String userName = (String) session.getAttribute("userName");
|
|
|
// 从会话中获取名为"sno"的属性值,并将其转换为String类型,该值表示学生编号
|
|
|
String sno = (String) session.getAttribute("sno");
|
|
|
|
|
|
// 将userName打印到控制台,用于调试
|
|
|
System.out.println(userName);
|
|
|
// 将sno打印到控制台,用于调试
|
|
|
System.out.println(sno);
|
|
|
// 打印一个字符串到控制台,可能是用于调试的无意义字符串
|
|
|
System.out.println("hdghghjg");
|
|
|
|
|
|
// 构造SQL查询语句,用于从student表中查询指定学生编号(sno)的学生信息
|
|
|
String sql = "select * from student where sno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,参数为学生编号
|
|
|
Object[] objects = {sno};
|
|
|
|
|
|
// 调用StuDao的qureyInfo方法执行SQL查询,并获取结果集
|
|
|
ResultSet resultSet = StuDao.qureyInfo(sql, objects);
|
|
|
// 创建一个ArrayList对象,用于存储查询到的学生对象
|
|
|
ArrayList<Student> stuArrayList = new ArrayList<Student>();
|
|
|
|
|
|
try {
|
|
|
// 遍历结果集,将每一行数据封装到Student对象中,并添加到ArrayList中
|
|
|
while (resultSet.next()){
|
|
|
// 创建一个新的Student对象
|
|
|
Student student = new Student();
|
|
|
// 设置学生编号
|
|
|
student.setSno(resultSet.getString("sno"));
|
|
|
// 设置学生姓名
|
|
|
student.setSname(resultSet.getString("sname"));
|
|
|
// 设置学生性别
|
|
|
student.setSsex(resultSet.getString("ssex"));
|
|
|
// 设置学生年龄,将数据库中的整数类型值转换为int类型
|
|
|
student.setSage(resultSet.getInt("sage"));
|
|
|
// 设置学生班级
|
|
|
student.setSclass(resultSet.getString("sclass"));
|
|
|
// 设置学生专业
|
|
|
student.setSpecialty(resultSet.getString("specialty"));
|
|
|
// 设置学生所在系部
|
|
|
student.setSdept(resultSet.getString("sdept"));
|
|
|
// 设置学生电话号码
|
|
|
student.setSphone(resultSet.getString("sphone"));
|
|
|
// 设置学生密码
|
|
|
student.setSpsw(resultSet.getString("spsw"));
|
|
|
// 将封装好的学生对象添加到ArrayList中
|
|
|
stuArrayList.add(student);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 捕获异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 无论是否发生异常,都调用JDBCUtils的close方法关闭结果集,释放资源
|
|
|
JDBCUtils.close(resultSet);
|
|
|
}
|
|
|
|
|
|
// 将存储学生对象的ArrayList打印到控制台,用于调试
|
|
|
System.out.println(stuArrayList);
|
|
|
|
|
|
// 将存储学生对象的ArrayList设置为请求属性,以便在后续页面中使用
|
|
|
req.setAttribute("stuArrayList", stuArrayList);
|
|
|
|
|
|
// 转发请求到指定的页面(/view/stu/stuinfo.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/stu/stuinfo.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);
|
|
|
}
|
|
|
}
|
|
|
package com.controller.stu;
|
|
|
// 声明该Servlet所在的包名为com.controller.stu
|
|
|
|
|
|
import com.dao.StuDao;
|
|
|
// 导入学生数据访问对象类,用于执行数据库的查询、统计等操作
|
|
|
|
|
|
import com.entity.PageBean;
|
|
|
// 导入分页数据封装的实体类,用于存储分页相关信息
|
|
|
|
|
|
import com.entity.StuPunch;
|
|
|
// 导入学生打卡信息的实体类,用于封装学生打卡的具体数据
|
|
|
|
|
|
import com.utils.JDBCUtils;
|
|
|
// 导入JDBC工具类,用于数据库连接的获取、关闭以及结果集的处理等操作
|
|
|
|
|
|
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 javax.servlet.http.HttpSession;
|
|
|
// 导入HttpSession类,用于管理用户会话,存储用户相关信息
|
|
|
|
|
|
import java.io.IOException;
|
|
|
// 导入用于处理I/O异常的类,如在读写数据时可能发生的异常
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入用于处理数据库查询结果的类
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
// 导入ArrayList类,用于存储多个对象,这里用于存储学生打卡信息对象
|
|
|
|
|
|
@WebServlet("/StuQueryPunchByPageServlet")
|
|
|
// 使用@WebServlet注解将该Servlet映射到URL路径为"/StuQueryPunchByPageServlet"
|
|
|
public class StuQueryPunchByPageServlet 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 spunchdate = req.getParameter("spunchdate");
|
|
|
|
|
|
// 如果获取的打卡日期为空,则设置为空字符串
|
|
|
if (spunchdate == null){
|
|
|
spunchdate = "";
|
|
|
}
|
|
|
|
|
|
// 对打卡日期进行模糊查询的格式处理,在前后添加通配符%
|
|
|
String spunchdate1 = "%" + spunchdate + "%";
|
|
|
// 打印处理后的打卡日期,用于调试
|
|
|
System.out.println(spunchdate1);
|
|
|
|
|
|
// 将原始的打卡日期设置为请求属性,以便在后续页面中使用
|
|
|
req.setAttribute("spunchdate", spunchdate);
|
|
|
|
|
|
// 获取登录时的session会话对象
|
|
|
HttpSession session = req.getSession();
|
|
|
// 从会话中获取名为"userName"的属性值,并将其转换为String类型
|
|
|
String userName = (String) session.getAttribute("userName");
|
|
|
// 从会话中获取名为"sno"的属性值,并将其转换为String类型,该值表示学生编号
|
|
|
String sno = (String) session.getAttribute("sno");
|
|
|
|
|
|
// 初始化SQL语句变量
|
|
|
String sql = null;
|
|
|
|
|
|
// 打印userName,用于调试
|
|
|
System.out.println(userName);
|
|
|
// 打印sno,用于调试
|
|
|
System.out.println(sno);
|
|
|
// 打印一个字符串,可能是用于调试的无意义字符串
|
|
|
System.out.println("hdghghjg");
|
|
|
|
|
|
// 从请求对象中获取当前页码
|
|
|
String currentPage = req.getParameter("currentPage");
|
|
|
// 从请求对象中获取每页显示的行数
|
|
|
String rows = req.getParameter("rows");
|
|
|
|
|
|
// 如果当前页码为空或为空字符串,则设置当前页码为1
|
|
|
if (currentPage == null || "".equals(currentPage)){
|
|
|
currentPage = "1";
|
|
|
}
|
|
|
|
|
|
// 如果每页显示的行数为空或为空字符串,则设置每页显示的行数为7
|
|
|
if (rows == null || "".equals(rows)){
|
|
|
rows = "7";
|
|
|
}
|
|
|
|
|
|
// 将当前页码转换为整数类型
|
|
|
int currentPage1 = Integer.parseInt(currentPage);
|
|
|
// 将每页显示的行数转换为整数类型
|
|
|
int rows1 = Integer.parseInt(rows);
|
|
|
|
|
|
// 如果当前页数小于1,则设置当前页数为1
|
|
|
if (currentPage1 <= 0){
|
|
|
currentPage1 = 1;
|
|
|
}
|
|
|
|
|
|
// 创建PageBean对象,泛型为StuPunch,用于存储分页信息和学生打卡信息列表
|
|
|
PageBean<StuPunch> pageBean = new PageBean<StuPunch>();
|
|
|
|
|
|
// 设置当前页码到PageBean对象中
|
|
|
pageBean.setCurrentPage(currentPage1);
|
|
|
|
|
|
// 设置每页的记录数到PageBean对象中
|
|
|
pageBean.setRows(rows1);
|
|
|
|
|
|
// 构造SQL语句,用于统计符合条件(打卡日期模糊匹配且学生编号匹配)的记录总数
|
|
|
sql = " select count(*) as num from student s,stupunchin sp where s.sno = sp.sno and sp.spunchdate like ? and s.sno = ?";
|
|
|
// 创建包含SQL语句参数的对象数组,参数为处理后的打卡日期和学生编号
|
|
|
Object[] objects = {spunchdate1, sno};
|
|
|
|
|
|
// 调用StuDao的findTotalCount方法执行SQL查询,获取总记录数
|
|
|
int totalCount = StuDao.findTotalCount(sql, objects);
|
|
|
// 打印总记录数,用于调试
|
|
|
System.out.println(totalCount);
|
|
|
// 设置总记录数到PageBean对象中
|
|
|
pageBean.setTotalCount(totalCount);
|
|
|
|
|
|
if (totalCount > 0){
|
|
|
// 计算总页码,如果总记录数能被每页记录数整除,则总页码为总记录数除以每页记录数;否则为商加1
|
|
|
int totalPage = (totalCount % rows1) == 0 ? totalCount/rows1 : (totalCount/rows1 + 1);
|
|
|
// 设置总页码到PageBean对象中
|
|
|
pageBean.setTotalPage(totalPage);
|
|
|
|
|
|
// 如果当前页数大于总页数,则将当前页数设置为总页数
|
|
|
if (currentPage1 > pageBean.getTotalPage()){
|
|
|
currentPage1 = pageBean.getTotalPage();
|
|
|
// 重新设置当前页码到PageBean对象中
|
|
|
pageBean.setCurrentPage(currentPage1);
|
|
|
}
|
|
|
|
|
|
// 计算开始的记录位置,为(当前页码 - 1)乘以每页记录数
|
|
|
int start = (currentPage1 - 1) * rows1;
|
|
|
// 构造SQL语句,用于查询符合条件的学生打卡信息,进行分页查询
|
|
|
sql = "select s.sname,sp.* from student s,stupunchin sp where s.sno = sp.sno and sp.spunchdate like ? and sp.sno = ? limit ?, ?";
|
|
|
// 创建包含SQL语句参数的对象数组,参数为处理后的打卡日期、学生编号、开始记录位置、每页记录数
|
|
|
Object[] objects1 = {spunchdate1, sno, start, rows1};
|
|
|
|
|
|
// 调用StuDao的QureyInfoByPage方法执行SQL查询,获取查询结果集
|
|
|
ResultSet resultSet = StuDao.QureyInfoByPage(sql, objects1);
|
|
|
// 创建ArrayList对象,用于存储查询到的学生打卡信息对象
|
|
|
ArrayList stuPunchArrayList = new ArrayList();
|
|
|
|
|
|
try {
|
|
|
// 遍历结果集,将每一行数据封装到StuPunch对象中,并添加到ArrayList中
|
|
|
while (resultSet.next()){
|
|
|
StuPunch stuPunch = new StuPunch();
|
|
|
stuPunch.setSname(resultSet.getString("sname"));
|
|
|
stuPunch.setSispunch(resultSet.getString("sispunch"));
|
|
|
stuPunch.setSpunchdate(resultSet.getDate("spunchdate"));
|
|
|
stuPunch.setSpunchtime(resultSet.getString("spunchtime"));
|
|
|
stuPunch.setSishot(resultSet.getString("sishot"));
|
|
|
stuPunch.setSiscough(resultSet.getString("siscough"));
|
|
|
stuPunch.setSisseem(resultSet.getString("sisseem"));
|
|
|
stuPunch.setSisdiagnose(resultSet.getString("sisdiagnose"));
|
|
|
stuPunch.setSstatus(resultSet.getString("sstatus"));
|
|
|
stuPunchArrayList.add(stuPunch);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 捕获异常并打印异常堆栈信息,用于调试和错误排查
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 无论是否发生异常,都调用JDBCUtils的close方法关闭结果集,释放资源
|
|
|
JDBCUtils.close(resultSet);
|
|
|
}
|
|
|
// 将存储学生打卡信息对象的ArrayList设置到PageBean对象中
|
|
|
pageBean.setArrayList(stuPunchArrayList);
|
|
|
|
|
|
// 打印存储学生打卡信息对象的ArrayList,用于调试
|
|
|
System.out.println(stuPunchArrayList);
|
|
|
// 打印PageBean对象,用于调试
|
|
|
System.out.println(pageBean);
|
|
|
|
|
|
// 将PageBean对象设置为请求属性,以便在后续页面中使用
|
|
|
req.setAttribute("pageBean", pageBean);
|
|
|
|
|
|
// 转发请求到指定的页面(/view/stu/stupunchinfo.jsp),并传递请求和响应对象
|
|
|
req.getRequestDispatcher("/view/stu/stupunchinfo.jsp").forward(req, resp);
|
|
|
}else {
|
|
|
// 如果总记录数为0,说明没有符合条件的数据,转发请求到无数据提示页面(/view/alluse/nodata.jsp)
|
|
|
req.getRequestDispatcher("/view/alluse/nodata.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);
|
|
|
}
|
|
|
}
|
|
|
package com.utils;
|
|
|
// 声明该类所在的包名为com.utils
|
|
|
|
|
|
import org.jfree.chart.JFreeChart;
|
|
|
// 导入JFreeChart类,它是JFreeChart库中表示图表的核心类
|
|
|
|
|
|
import org.jfree.chart.block.BlockBorder;
|
|
|
// 导入BlockBorder类,用于设置图表中块(如图例)的边框
|
|
|
|
|
|
import org.jfree.data.category.DefaultCategoryDataset;
|
|
|
// 导入DefaultCategoryDataset类,用于处理柱状图、折线图等类别数据的数据集
|
|
|
|
|
|
import org.jfree.data.general.DefaultPieDataset;
|
|
|
// 导入DefaultPieDataset类,用于处理饼图的数据集
|
|
|
|
|
|
import org.jfree.ui.RectangleEdge;
|
|
|
// 导入RectangleEdge类,用于指定图表中元素(如图例)的位置,如在矩形的哪条边上
|
|
|
|
|
|
import java.awt.*;
|
|
|
// 导入java.awt包,包含用于处理图形和用户界面的类
|
|
|
|
|
|
import java.util.Vector;
|
|
|
// 导入Vector类,它是一个动态数组,用于存储数据系列等
|
|
|
|
|
|
/**
|
|
|
* 生成JFreeChart图表的工厂类<br/>
|
|
|
* 目的:根据MVC的设计思想,数据与展现分离。调用者只需传入数据,即可生成图表。
|
|
|
*
|
|
|
* @author liuyimin
|
|
|
*
|
|
|
*/
|
|
|
public class ChartFactory {
|
|
|
/**
|
|
|
* 生成柱状图
|
|
|
*
|
|
|
* @param title
|
|
|
* 柱状图的标题
|
|
|
* @param categoryAxisLabel
|
|
|
* x轴标题
|
|
|
* @param valueAxisLabel
|
|
|
* y轴标题
|
|
|
* @param series
|
|
|
* 数据
|
|
|
* @param categories
|
|
|
* 类别
|
|
|
* @return
|
|
|
*/
|
|
|
public static JFreeChart createBarChart(String title,
|
|
|
String categoryAxisLabel, String valueAxisLabel,
|
|
|
Vector<Serie> series, String[] categories) {
|
|
|
// 1:创建数据集合
|
|
|
// 调用ChartUtils的createDefaultCategoryDataset方法,根据传入的数据系列和类别创建一个默认的类别数据集
|
|
|
DefaultCategoryDataset dataset = ChartUtils
|
|
|
.createDefaultCategoryDataset(series, categories);
|
|
|
// 使用JFreeChart的工厂方法创建一个柱状图,传入标题、x轴标题、y轴标题和数据集
|
|
|
JFreeChart chart = org.jfree.chart.ChartFactory.createBarChart(title,
|
|
|
categoryAxisLabel, valueAxisLabel, dataset);
|
|
|
// 3:设置抗锯齿,防止字体显示不清楚
|
|
|
// 调用ChartUtils的setAntiAlias方法,为图表设置抗锯齿,使字体等显示更清晰
|
|
|
ChartUtils.setAntiAlias(chart);// 抗锯齿
|
|
|
// 4:对柱子进行渲染
|
|
|
// 调用ChartUtils的setBarRenderer方法,对柱状图的柱子进行渲染,第二个参数false可能表示某种渲染相关的设置
|
|
|
ChartUtils.setBarRenderer(chart.getCategoryPlot(), false);//
|
|
|
// 5:对其他部分进行渲染
|
|
|
// 调用ChartUtils的setXAixs方法,对图表的X坐标轴进行渲染
|
|
|
ChartUtils.setXAixs(chart.getCategoryPlot());// X坐标轴渲染
|
|
|
// 调用ChartUtils的setYAixs方法,对图表的Y坐标轴进行渲染
|
|
|
ChartUtils.setYAixs(chart.getCategoryPlot());// Y坐标轴渲染
|
|
|
// 设置标注无边框
|
|
|
// 创建一个白色边框的BlockBorder对象,并将其设置为图表图例的边框
|
|
|
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
|
|
|
// 返回创建并渲染好的柱状图
|
|
|
return chart;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成饼图
|
|
|
*
|
|
|
* @param title
|
|
|
* 饼图的标题
|
|
|
* @param categories
|
|
|
* 类别
|
|
|
* @param datas
|
|
|
* 数据
|
|
|
* @return
|
|
|
*/
|
|
|
public static JFreeChart createPieChart(String title, String[] categories,
|
|
|
Object[] datas) {
|
|
|
// 1:创建数据集合
|
|
|
// 调用ChartUtils的createDefaultPieDataset方法,根据传入的类别和数据创建一个默认的饼图数据集
|
|
|
DefaultPieDataset dataset = ChartUtils.createDefaultPieDataset(
|
|
|
categories, datas);
|
|
|
// 使用JFreeChart的工厂方法创建一个饼图,传入标题和数据集
|
|
|
JFreeChart chart = org.jfree.chart.ChartFactory.createPieChart(title,
|
|
|
dataset);
|
|
|
// 3:设置抗锯齿,防止字体显示不清楚
|
|
|
// 调用ChartUtils的setAntiAlias方法,为图表设置抗锯齿,使字体等显示更清晰
|
|
|
ChartUtils.setAntiAlias(chart);// 抗锯齿
|
|
|
// 4:对柱子进行渲染[创建不同图形]
|
|
|
// 调用ChartUtils的setPieRender方法,对饼图的绘图区域进行渲染
|
|
|
ChartUtils.setPieRender(chart.getPlot());
|
|
|
/**
|
|
|
* 可以注释测试
|
|
|
*/
|
|
|
// plot.setSimpleLabels(true);//简单标签,不绘制线条
|
|
|
// plot.setLabelGenerator(null);//不显示数字
|
|
|
// 设置标注无边框
|
|
|
// 创建一个白色边框的BlockBorder对象,并将其设置为图表图例的边框
|
|
|
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
|
|
|
// 将图表的图例位置设置为右侧
|
|
|
chart.getLegend().setPosition(RectangleEdge.RIGHT);
|
|
|
// 返回创建并渲染好的饼图
|
|
|
return chart;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成折线图
|
|
|
*
|
|
|
* @param title
|
|
|
* 折线图的标题
|
|
|
* @param categoryAxisLabel
|
|
|
* x轴标题
|
|
|
* @param valueAxisLabel
|
|
|
* y轴标题
|
|
|
* @param series
|
|
|
* 数据
|
|
|
* @param categories
|
|
|
* 类别
|
|
|
* @return
|
|
|
*/
|
|
|
public static JFreeChart createLineChart(String title,
|
|
|
String categoryAxisLabel, String valueAxisLabel,
|
|
|
Vector<Serie> series, String[] categories) {
|
|
|
// 1:创建数据集合
|
|
|
// 调用ChartUtils的createDefaultCategoryDataset方法,根据传入的数据系列和类别创建一个默认的类别数据集
|
|
|
DefaultCategoryDataset dataset = ChartUtils
|
|
|
.createDefaultCategoryDataset(series, categories);
|
|
|
// 使用JFreeChart的工厂方法创建一个折线图,传入标题、x轴标题、y轴标题和数据集
|
|
|
JFreeChart chart = org.jfree.chart.ChartFactory.createLineChart(title,
|
|
|
categoryAxisLabel, valueAxisLabel, dataset);
|
|
|
// 3:设置抗锯齿,防止字体显示不清楚
|
|
|
// 调用ChartUtils的setAntiAlias方法,为图表设置抗锯齿,使字体等显示更清晰
|
|
|
ChartUtils.setAntiAlias(chart);// 抗锯齿
|
|
|
// 4:对柱子进行渲染[[采用不同渲染]]
|
|
|
// 调用ChartUtils的setLineRender方法,对折线图的绘图区域进行渲染,后面的参数false和true可能表示某种渲染相关的设置
|
|
|
ChartUtils.setLineRender(chart.getCategoryPlot(), false, true);//
|
|
|
// 5:对其他部分进行渲染
|
|
|
// 调用ChartUtils的setXAixs方法,对图表的X坐标轴进行渲染
|
|
|
ChartUtils.setXAixs(chart.getCategoryPlot());// X坐标轴渲染
|
|
|
// 调用ChartUtils的setYAixs方法,对图表的Y坐标轴进行渲染
|
|
|
ChartUtils.setYAixs(chart.getCategoryPlot());// Y坐标轴渲染
|
|
|
// 设置标注无边框
|
|
|
// 创建一个白色边框的BlockBorder对象,并将其设置为图表图例的边框
|
|
|
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
|
|
|
// 返回创建并渲染好的折线图
|
|
|
return chart;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成StackedBarChart
|
|
|
*
|
|
|
* @param title
|
|
|
* StackedBarChart的标题
|
|
|
* @param domainAxisLabel
|
|
|
* @param rangeAxisLabel
|
|
|
* @param series
|
|
|
* 数据
|
|
|
* @param categories
|
|
|
* 类别
|
|
|
* @return
|
|
|
*/
|
|
|
public static JFreeChart createStackedBarChart(String title,
|
|
|
String domainAxisLabel, String rangeAxisLabel,
|
|
|
Vector<Serie> series, String[] categories) {
|
|
|
// 1:创建数据集合
|
|
|
// 调用ChartUtils的createDefaultCategoryDataset方法,根据传入的数据系列和类别创建一个默认的类别数据集
|
|
|
DefaultCategoryDataset dataset = ChartUtils
|
|
|
.createDefaultCategoryDataset(series, categories);
|
|
|
// 使用JFreeChart的工厂方法创建一个堆积柱状图,传入标题、x轴标题、y轴标题和数据集
|
|
|
JFreeChart chart = org.jfree.chart.ChartFactory.createStackedBarChart(
|
|
|
title, domainAxisLabel, rangeAxisLabel, dataset);
|
|
|
// 3:设置抗锯齿,防止字体显示不清楚
|
|
|
// 调用ChartUtils的setAntiAlias方法,为图表设置抗锯齿,使字体等显示更清晰
|
|
|
ChartUtils.setAntiAlias(chart);// 抗锯齿
|
|
|
// 4:对柱子进行渲染[创建不同图形]
|
|
|
// 调用ChartUtils的setStackBarRender方法,对堆积柱状图的绘图区域进行渲染
|
|
|
ChartUtils.setStackBarRender(chart.getCategoryPlot());
|
|
|
// 5:对其他部分进行渲染
|
|
|
// 调用ChartUtils的setXAixs方法,对图表的X坐标轴进行渲染
|
|
|
ChartUtils.setXAixs(chart.getCategoryPlot());// X坐标轴渲染
|
|
|
// 调用ChartUtils的setYAixs方法,对图表的Y坐标轴进行渲染
|
|
|
ChartUtils.setYAixs(chart.getCategoryPlot());// Y坐标轴渲染
|
|
|
// 设置标注无边框
|
|
|
// 创建一个白色边框的BlockBorder对象,并将其设置为图表图例的边框
|
|
|
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
|
|
|
// 返回创建并渲染好的堆积柱状图
|
|
|
return chart;
|
|
|
}
|
|
|
}
|
|
|
package com.utils;
|
|
|
// 声明该类所在的包名为com.utils
|
|
|
|
|
|
import org.jfree.chart.ChartFactory;
|
|
|
// 导入JFreeChart库中的图表工厂类,用于创建各种类型的图表
|
|
|
|
|
|
import org.jfree.chart.JFreeChart;
|
|
|
// 导入JFreeChart类,它是JFreeChart库中表示图表的核心类
|
|
|
|
|
|
import org.jfree.chart.StandardChartTheme;
|
|
|
// 导入标准图表主题类,用于设置图表的外观主题
|
|
|
|
|
|
import org.jfree.chart.axis.DateAxis;
|
|
|
// 导入日期轴类,用于在图表中显示日期
|
|
|
|
|
|
import org.jfree.chart.axis.DateTickUnit;
|
|
|
// 导入日期刻度单位类,用于定义日期轴上的刻度间隔
|
|
|
|
|
|
import org.jfree.chart.axis.DateTickUnitType;
|
|
|
// 导入日期刻度单位类型枚举类,用于指定日期刻度的类型(如年、月、日等)
|
|
|
|
|
|
import org.jfree.chart.axis.ValueAxis;
|
|
|
// 导入值轴类,用于表示图表中的数值轴
|
|
|
|
|
|
import org.jfree.chart.block.BlockBorder;
|
|
|
// 导入块边框类,用于设置图表中块(如图例)的边框
|
|
|
|
|
|
import org.jfree.chart.labels.*;
|
|
|
// 导入JFreeChart中用于标签相关的类的包,用于设置图表的标签显示等
|
|
|
|
|
|
import org.jfree.chart.plot.*;
|
|
|
// 导入JFreeChart中用于绘图区域相关的类的包,用于处理图表的绘图区域设置
|
|
|
|
|
|
import org.jfree.chart.renderer.category.BarRenderer;
|
|
|
// 导入柱状图渲染器类,用于渲染柱状图
|
|
|
|
|
|
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
|
|
|
// 导入折线图和形状渲染器类,用于渲染折线图和数据点的形状
|
|
|
|
|
|
import org.jfree.chart.renderer.category.StackedBarRenderer;
|
|
|
// 导入堆积柱状图渲染器类,用于渲染堆积柱状图
|
|
|
|
|
|
import org.jfree.chart.renderer.category.StandardBarPainter;
|
|
|
// 导入标准柱状图绘制器类,用于绘制柱状图的外观
|
|
|
|
|
|
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
|
|
|
// 导入标准XY柱状图绘制器类,用于绘制XY图表中的柱状图外观
|
|
|
|
|
|
import org.jfree.chart.renderer.xy.XYBarRenderer;
|
|
|
// 导入XY柱状图渲染器类,用于渲染XY图表中的柱状图
|
|
|
|
|
|
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
|
|
|
// 导入XY折线图和形状渲染器类,用于渲染XY图表中的折线图和数据点的形状
|
|
|
|
|
|
import org.jfree.data.category.DefaultCategoryDataset;
|
|
|
// 导入默认的类别数据集类,用于处理柱状图、折线图等类别数据的数据集
|
|
|
|
|
|
import org.jfree.data.general.DefaultPieDataset;
|
|
|
// 导入默认的饼图数据集类,用于处理饼图的数据集
|
|
|
|
|
|
import org.jfree.data.time.Day;
|
|
|
// 导入表示一天的时间类,用于时间序列数据
|
|
|
|
|
|
import org.jfree.data.time.TimeSeries;
|
|
|
// 导入时间序列类,用于处理时间序列数据
|
|
|
|
|
|
import org.jfree.ui.RectangleInsets;
|
|
|
// 导入矩形边距类,用于设置图表中元素的边距
|
|
|
|
|
|
import org.jfree.ui.TextAnchor;
|
|
|
// 导入文本锚点类,用于指定文本的位置
|
|
|
|
|
|
import java.awt.*;
|
|
|
// 导入java.awt包,包含用于处理图形和用户界面的类
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
// 导入十进制格式化类,用于格式化数字
|
|
|
|
|
|
import java.text.NumberFormat;
|
|
|
// 导入数字格式化类,用于格式化数字
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
// 导入解析异常类,用于处理日期等解析时可能抛出的异常
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
// 导入简单日期格式化类,用于格式化日期
|
|
|
|
|
|
import java.util.Date;
|
|
|
// 导入日期类,用于表示日期和时间
|
|
|
|
|
|
import java.util.Vector;
|
|
|
// 导入Vector类,它是一个动态数组,用于存储数据系列等
|
|
|
|
|
|
/**
|
|
|
* Jfreechart工具类
|
|
|
* <p>
|
|
|
* 解决中文乱码问题<br>
|
|
|
* 用来创建类别图表数据集、创建饼图数据集、时间序列图数据集<br>
|
|
|
* 用来对柱状图、折线图、饼图、堆积柱状图、时间序列图的样式进行渲染<br>
|
|
|
* 设置X-Y坐标轴样式
|
|
|
* <p>
|
|
|
*
|
|
|
*
|
|
|
* @author chenchangwen
|
|
|
* @since:2014-2-18
|
|
|
*
|
|
|
*/
|
|
|
public class ChartUtils {
|
|
|
// 定义一个静态字符串,用于表示无数据时的提示信息
|
|
|
private static String NO_DATA_MSG = "暂无数据";
|
|
|
// 定义一个静态字体对象,设置字体为宋体,样式为普通,大小为12
|
|
|
private static Font FONT = new Font("宋体", Font.PLAIN, 12);
|
|
|
// 定义一个静态颜色数组,包含多种颜色,用于图表的颜色设置
|
|
|
public static Color[] CHART_COLORS = { new Color(31, 129, 188),
|
|
|
new Color(92, 92, 97), new Color(144, 237, 125),
|
|
|
new Color(255, 188, 117), new Color(153, 158, 255),
|
|
|
new Color(255, 117, 153), new Color(253, 236, 109),
|
|
|
new Color(128, 133, 232), new Color(158, 90, 102),
|
|
|
new Color(255, 204, 102) };
|
|
|
|
|
|
// 静态代码块,在类加载时执行,用于设置图表主题
|
|
|
static {
|
|
|
setChartTheme();
|
|
|
}
|
|
|
|
|
|
// 空的构造函数
|
|
|
public ChartUtils() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 中文主题样式 解决乱码
|
|
|
*/
|
|
|
public static void setChartTheme() {
|
|
|
// 创建一个标准图表主题对象,命名为"CN"
|
|
|
StandardChartTheme chartTheme = new StandardChartTheme("CN");
|
|
|
// 设置标题字体为之前定义的字体
|
|
|
chartTheme.setExtraLargeFont(FONT);
|
|
|
// 设置图例的字体为之前定义的字体
|
|
|
chartTheme.setRegularFont(FONT);
|
|
|
// 设置轴向的字体为之前定义的字体
|
|
|
chartTheme.setLargeFont(FONT);
|
|
|
chartTheme.setSmallFont(FONT);
|
|
|
// 设置标题的颜色为灰色
|
|
|
chartTheme.setTitlePaint(new Color(51, 51, 51));
|
|
|
// 设置副标题的颜色为深灰色
|
|
|
chartTheme.setSubtitlePaint(new Color(85, 85, 85));
|
|
|
|
|
|
// 设置图例的背景颜色为白色
|
|
|
chartTheme.setLegendBackgroundPaint(Color.WHITE);
|
|
|
// 设置图例项的颜色为黑色
|
|
|
chartTheme.setLegendItemPaint(Color.BLACK);
|
|
|
// 设置图表的背景颜色为白色
|
|
|
chartTheme.setChartBackgroundPaint(Color.WHITE);
|
|
|
// 绘制颜色绘制颜色.轮廓供应商
|
|
|
// paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
|
|
|
|
|
|
// 定义轮廓颜色数组,这里只有白色
|
|
|
Paint[] OUTLINE_PAINT_SEQUENCE = new Paint[] { Color.WHITE };
|
|
|
// 创建一个默认的绘图供应商,设置颜色、轮廓颜色等
|
|
|
DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(
|
|
|
CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
|
|
|
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
|
|
|
DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
|
|
|
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
|
|
|
// 设置图表主题的绘图供应商
|
|
|
chartTheme.setDrawingSupplier(drawingSupplier);
|
|
|
|
|
|
// 设置绘图区域的背景颜色为白色
|
|
|
chartTheme.setPlotBackgroundPaint(Color.WHITE);
|
|
|
// 设置绘图区域外边框的颜色为白色
|
|
|
chartTheme.setPlotOutlinePaint(Color.WHITE);
|
|
|
// 设置链接标签的颜色
|
|
|
chartTheme.setLabelLinkPaint(new Color(8, 55, 114));
|
|
|
// 设置标签链接的样式为三次曲线
|
|
|
chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
|
|
|
|
|
|
// 设置图表的边距
|
|
|
chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12));
|
|
|
// 设置X坐标轴垂直网格的颜色
|
|
|
chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));
|
|
|
// 设置Y坐标轴水平网格的颜色
|
|
|
chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));
|
|
|
|
|
|
// 设置基线的颜色为白色
|
|
|
chartTheme.setBaselinePaint(Color.WHITE);
|
|
|
// 设置十字准线的颜色为蓝色
|
|
|
chartTheme.setCrosshairPaint(Color.BLUE);
|
|
|
// 设置坐标轴标题文字的颜色
|
|
|
chartTheme.setAxisLabelPaint(new Color(51, 51, 51));
|
|
|
// 设置刻度数字的颜色
|
|
|
chartTheme.setTickLabelPaint(new Color(67, 67, 72));
|
|
|
// 设置柱状图的渲染器为标准柱状图绘制器
|
|
|
chartTheme.setBarPainter(new StandardBarPainter());
|
|
|
// 设置XYBar的渲染器为标准XYBar绘制器
|
|
|
chartTheme.setXYBarPainter(new StandardXYBarPainter());
|
|
|
|
|
|
// 设置项目标签的颜色为黑色
|
|
|
chartTheme.setItemLabelPaint(Color.black);
|
|
|
// 设置温度计的颜色为白色
|
|
|
chartTheme.setThermometerPaint(Color.white);
|
|
|
|
|
|
// 设置JFreeChart的全局图表主题
|
|
|
ChartFactory.setChartTheme(chartTheme);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 必须设置文本抗锯齿
|
|
|
*/
|
|
|
public static void setAntiAlias(JFreeChart chart) {
|
|
|
// 设置图表的文本抗锯齿为关闭状态
|
|
|
chart.setTextAntiAlias(false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置图例无边框,默认黑色边框
|
|
|
*/
|
|
|
public static void setLegendEmptyBorder(JFreeChart chart) {
|
|
|
// 创建一个白色边框的BlockBorder对象,并将其设置为图表图例的边框
|
|
|
chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建类别数据集合
|
|
|
*/
|
|
|
public static DefaultCategoryDataset createDefaultCategoryDataset(
|
|
|
Vector<Serie> series, String[] categories) {
|
|
|
// 创建一个默认的类别数据集对象
|
|
|
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
|
|
|
|
|
|
// 遍历数据系列
|
|
|
for (Serie serie : series) {
|
|
|
// 获取数据系列的名称
|
|
|
String name = serie.getName();
|
|
|
// 获取数据系列的数据
|
|
|
Vector<Object> data = serie.getData();
|
|
|
// 如果数据和类别都不为空,且数据数量和类别数量相等
|
|
|
if (data != null && categories != null
|
|
|
&& data.size() == categories.length) {
|
|
|
// 遍历数据
|
|
|
for (int index = 0; index < data.size(); index++) {
|
|
|
// 获取数据值,并处理为字符串
|
|
|
String value = data.get(index) == null? "" : data.get(
|
|
|
index).toString();
|
|
|
// 如果数据是百分比形式
|
|
|
if (isPercent(value)) {
|
|
|
// 去掉百分比符号
|
|
|
value = value.substring(0, value.length() - 1);
|
|
|
}
|
|
|
// 如果数据是数字
|
|
|
if (isNumber(value)) {
|
|
|
// 将数据值添加到数据集中
|
|
|
dataset.setValue(Double.parseDouble(value), name,
|
|
|
categories[index]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 返回创建好的数据集
|
|
|
return dataset;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建饼图数据集合
|
|
|
*/
|
|
|
public static DefaultPieDataset createDefaultPieDataset(
|
|
|
String[] categories, Object[] datas) {
|
|
|
// 创建一个默认的饼图数据集对象
|
|
|
DefaultPieDataset dataset = new DefaultPieDataset();
|
|
|
// 遍历类别
|
|
|
for (int i = 0; i < categories.length && categories != null; i++) {
|
|
|
// 获取数据值,并处理为字符串
|
|
|
String value = datas[i].toString();
|
|
|
// 如果数据是百分比形式
|
|
|
if (isPercent(value)) {
|
|
|
// 去掉百分比符号
|
|
|
value = value.substring(0, value.length() - 1);
|
|
|
}
|
|
|
// 如果数据是数字
|
|
|
if (isNumber(value)) {
|
|
|
// 将数据值添加到数据集中
|
|
|
dataset.setValue(categories[i], Double.valueOf(value));
|
|
|
}
|
|
|
}
|
|
|
// 返回创建好的数据集
|
|
|
return dataset;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建时间序列数据
|
|
|
*
|
|
|
* @param category
|
|
|
* 类别
|
|
|
* @param dateValues
|
|
|
* 日期-值 数组
|
|
|
* @param xAxisTitle
|
|
|
* X坐标轴标题
|
|
|
* @return
|
|
|
*/
|
|
|
public static TimeSeries createTimeseries(String category,
|
|
|
Vector<Object[]> dateValues) {
|
|
|
// 创建一个时间序列对象,传入类别名称
|
|
|
TimeSeries timeseries = new TimeSeries(category);
|
|
|
|
|
|
// 如果日期值数组不为空
|
|
|
if (dateValues != null) {
|
|
|
// 创建一个简单日期格式化对象,格式为"yyyy-MM-dd"
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// 遍历日期值数组
|
|
|
for (Object[] objects : dateValues) {
|
|
|
// 初始化日期对象
|
|
|
Date date = null;
|
|
|
try {
|
|
|
// 解析日期字符串为日期对象
|
|
|
date = dateFormat.parse(objects[0].toString());
|
|
|
} catch (ParseException e) {
|
|
|
}
|
|
|
// 获取数据值,并处理为字符串
|
|
|
String sValue = objects[1].toString();
|
|
|
// 初始化数据值为0
|
|
|
double dValue = 0;
|
|
|
// 如果日期不为空且数据是数字
|
|
|
if (date != null && isNumber(sValue)) {
|
|
|
// 将数据值转换为double类型
|
|
|
dValue = Double.parseDouble(sValue);
|
|
|
// 将数据添加到时间序列中
|
|
|
timeseries.add(new Day(date), dValue);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回创建好的时间序列对象
|
|
|
return timeseries;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置 折线图样式
|
|
|
*
|
|
|
* @param plot
|
|
|
* @param isShowDataLabels
|
|
|
* 是否显示数据标签 默认不显示节点形状
|
|
|
*/
|
|
|
public static void setLineRender(CategoryPlot plot, boolean isShowDataLabels) {
|
|
|
// 调用另一个设置折线图样式的方法,不显示节点形状
|
|
|
setLineRender(plot, isShowDataLabels, false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置折线图样式
|
|
|
*
|
|
|
* @param plot
|
|
|
* @param isShowDataLabels
|
|
|
* 是否显示数据标签
|
|
|
*/
|
|
|
@SuppressWarnings("deprecation")
|
|
|
public static void setLineRender(CategoryPlot plot,
|
|
|
boolean isShowDataLabels, boolean isShapesVisible) {
|
|
|
// 设置无数据时的提示信息
|
|
|
plot.setNoDataMessage(NO_DATA_MSG);
|
|
|
// 设置绘图区域的边距
|
|
|
plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);
|
|
|
// 获取折线图的渲染器
|
|
|
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
|
|
|
.getRenderer();
|
|
|
|
|
|
// 设置线条的粗细为1.5F
|
|
|
renderer.setStroke(new BasicStroke(1.5F));
|
|
|
// 如果要显示数据标签
|
|
|
if (isShowDataLabels) {
|
|
|
// 设置基本项目标签可见
|
|
|
renderer.setBaseItemLabelsVisible(true);
|
|
|
// 设置基本项目标签生成器
|
|
|
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(
|
|
|
StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,
|
|
|
NumberFormat.getInstance()));
|
|
|
// 设置基本正项目标签的位置
|
|
|
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
|
|
|
ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));
|
|
|
}
|
|
|
// 设置数据点绘制形状的可见性
|
|
|
renderer.setBaseShapesVisible(isShapesVisible);
|
|
|
// 设置X坐标轴样式
|
|
|
setXAixs(plot);
|
|
|
// 设置Y坐标轴样式
|
|
|
setYAixs(plot);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置时间序列图样式
|
|
|
*
|
|
|
* @param plot
|
|
|
* @param isShowData
|
|
|
* 是否显示数据
|
|
|
* @param isShapesVisible
|
|
|
* 是否显示数据节点形状
|
|
|
*/
|
|
|
public static void setTimeSeriesRender(Plot plot, boolean isShowData,
|
|
|
boolean isShapesVisible) {
|
|
|
|
|
|
// 将绘图区域转换为XYPlot类型
|
|
|
XYPlot xyplot = (XYPlot) plot;
|
|
|
// 设置无数据时的提示信息
|
|
|
xyplot.setNoDataMessage(NO_DATA_MSG);
|
|
|
// 设置绘图区域的边距
|
|
|
xyplot.setInsets(new RectangleInsets(10, 10, 5, 10));
|
|
|
|
|
|
// 获取XY折线图和形状渲染器
|
|
|
XYLineAndShapeRenderer xyRenderer = (XYLineAndShapeRenderer) xyplot
|
|
|
.getRenderer();
|
|
|
|
|
|
// 设置基本项目标签生成器
|
|
|
xyRenderer
|
|
|
.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
|
|
|
// 设置基本数据点形状不可见
|
|
|
xyRenderer.setBaseShapesVisible(false);
|
|
|
// 如果要显示数据
|
|
|
if (isShowData) {
|
|
|
// 设置基本项目标签可见
|
|
|
xyRenderer.setBaseItemLabelsVisible(true);
|
|
|
// 设置基本项目标签生成器
|
|
|
xyRenderer
|
|
|
.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
|
|
|
// 设置基本正项目标签的位置
|
|
|
xyRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
|
|
|
ItemLabelAnchor.OUTSIDE1, TextAnchor.BOTTOM_CENTER));
|
|
|
}
|
|
|
// 设置数据点绘制形状的可见性
|
|
|
xyRenderer.setBaseShapesVisible(isShapesVisible);
|
|
|
|
|
|
//
|
|
|
package com.utils;
|
|
|
// 声明该类所在的包名为com.utils
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
// 导入java.sql包中的Connection类,用于表示与数据库的连接
|
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
// 导入java.sql包中的DriverManager类,用于管理数据库驱动程序
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
// 导入java.sql包中的ResultSet类,用于处理数据库查询结果集
|
|
|
|
|
|
import java.sql.Statement;
|
|
|
// 导入java.sql包中的Statement类,用于执行SQL语句
|
|
|
|
|
|
/**
|
|
|
* JDBC工具类,提供数据库连接和资源关闭的静态方法
|
|
|
*/
|
|
|
public class JDBCUtils {
|
|
|
|
|
|
// 数据库驱动类名,这里使用的是MySQL的旧驱动
|
|
|
private static String driver = "com.mysql.jdbc.Driver";
|
|
|
// 数据库连接URL,指定了连接的主机、端口和数据库名
|
|
|
private static String url = "jdbc:mysql://localhost:3306/lsyqfksys";
|
|
|
// 数据库用户名
|
|
|
private static String user = "root";
|
|
|
// 数据库密码
|
|
|
private static String psw = "105293";
|
|
|
|
|
|
/**
|
|
|
* 获取数据库连接
|
|
|
* @return 数据库连接对象,如果连接失败则返回null
|
|
|
*/
|
|
|
public static Connection getConnection(){
|
|
|
// 声明数据库连接对象
|
|
|
Connection connection = null;
|
|
|
try {
|
|
|
// 加载数据库驱动类
|
|
|
Class.forName(driver);
|
|
|
// 通过DriverManager获取数据库连接
|
|
|
connection = DriverManager.getConnection(url, user, psw);
|
|
|
// 打印连接成功信息
|
|
|
System.out.println("连接成功");
|
|
|
}
|
|
|
catch (Exception e){
|
|
|
// 打印异常堆栈信息
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
// 返回数据库连接对象
|
|
|
return connection;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 释放查询操作相关的资源(ResultSet, Statement, Connection)
|
|
|
* @param resultSet 结果集对象
|
|
|
* @param statement SQL语句执行对象
|
|
|
* @param connection 数据库连接对象
|
|
|
*/
|
|
|
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
|
|
|
// 关闭结果集
|
|
|
if (resultSet != null) {
|
|
|
try {
|
|
|
resultSet.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 关闭Statement对象
|
|
|
if (statement != null) {
|
|
|
try {
|
|
|
statement.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 关闭数据库连接
|
|
|
if (connection != null) {
|
|
|
try {
|
|
|
connection.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 释放更新操作相关的资源(Statement, Connection)
|
|
|
* @param statement SQL语句执行对象
|
|
|
* @param connection 数据库连接对象
|
|
|
*/
|
|
|
public static void close(Statement statement, Connection connection) {
|
|
|
// 关闭Statement对象
|
|
|
if (statement != null) {
|
|
|
try {
|
|
|
statement.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 关闭数据库连接
|
|
|
if (connection != null) {
|
|
|
try {
|
|
|
connection.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 关闭结果集资源
|
|
|
* @param resultSet 结果集对象
|
|
|
*/
|
|
|
public static void close(ResultSet resultSet) {
|
|
|
// 关闭结果集
|
|
|
if (resultSet != null) {
|
|
|
try {
|
|
|
resultSet.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}package com.utils;
|
|
|
// 声明该类所在的包名为com.utils
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
// 导入java.io包中的Serializable接口,用于实现对象的序列化
|
|
|
|
|
|
import java.util.Vector;
|
|
|
// 导入java.util包中的Vector类,用于存储数据集合
|
|
|
|
|
|
/**
|
|
|
* 系列:名字和数据集合 构成一条曲线</br> 可以将serie看作一根线或者一根柱子:
|
|
|
*
|
|
|
* <p>
|
|
|
* 参照JS图表来描述数据:</br> series: [{ name: 'Tokyo', data: [7.0, 6.9, 9.5, 14.5]
|
|
|
* },</br> { name: 'New York', data: [-0.2, 0.8, 5.7, 11.3} ]</br>
|
|
|
* </p>
|
|
|
*
|
|
|
* @author ccw
|
|
|
* @date 2014-6-4
|
|
|
*/
|
|
|
public class Serie implements Serializable {
|
|
|
|
|
|
// 序列化版本号,用于确保序列化和反序列化的兼容性
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
// 系列的名称,例如在图表中表示一条线或一个柱子的名称
|
|
|
private String name;
|
|
|
// 系列的数据集合,使用Vector存储各种类型的数据值
|
|
|
private Vector<Object> data;
|
|
|
|
|
|
// 无参构造函数
|
|
|
public Serie() {
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param name
|
|
|
* 名称(线条名称)
|
|
|
* @param data
|
|
|
* 数据(线条上的所有数据值)
|
|
|
*/
|
|
|
public Serie(String name, Vector<Object> data) {
|
|
|
// 初始化系列名称
|
|
|
this.name = name;
|
|
|
// 初始化系列数据
|
|
|
this.data = data;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param name
|
|
|
* 名称(线条名称)
|
|
|
* @param array
|
|
|
* 数据(线条上的所有数据值)
|
|
|
*/
|
|
|
public Serie(String name, Object[] array) {
|
|
|
// 初始化系列名称
|
|
|
this.name = name;
|
|
|
// 如果传入的数据数组不为空
|
|
|
if (array != null) {
|
|
|
// 创建一个初始容量为数组长度的Vector
|
|
|
data = new Vector<Object>(array.length);
|
|
|
// 遍历数组,将元素添加到Vector中
|
|
|
for (int i = 0; i < array.length; i++) {
|
|
|
data.add(array[i]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取系列名称的方法
|
|
|
public String getName() {
|
|
|
return name;
|
|
|
}
|
|
|
|
|
|
// 设置系列名称的方法
|
|
|
public void setName(String name) {
|
|
|
this.name = name;
|
|
|
}
|
|
|
|
|
|
// 获取系列数据集合的方法
|
|
|
public Vector<Object> getData() {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
// 设置系列数据集合的方法
|
|
|
public void setData(Vector<Object> data) {
|
|
|
this.data = data;
|
|
|
}
|
|
|
} |