登录界面 分为职员,管理员、部门经理
`<%--
Created by IntelliJ IDEA.
User: 32262
Date: 2024/12/16
Time: 14:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
package org.example.kaoqin;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/userLogin")
public class Userloginservlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws javax.servlet.ServletException, IOException {
String name = req.getParameter("name");
String password = req.getParameter("password");
// 获取 HttpSession 对象
HttpSession session = req.getSession();
try (Connection connection = Database.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT role FROM zhiyuan WHERE name = ? AND password = ?")) {
statement.setString(1, name);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
// 获取用户身份
String role = resultSet.getString("role");
// 将用户名和身份存储在 session 中
session.setAttribute("name", name);
session.setAttribute("role", role);
// 根据身份转发到相应的页面
if ("zhiyuan".equals(role)) {
req.getRequestDispatcher("zhiyuan.jsp").forward(req, resp);
} else if ("bumenjingli".equals(role)) {
req.getRequestDispatcher("bumenjingli.jsp").forward(req, resp);
} else if ("guanliyuan".equals(role)) {
req.getRequestDispatcher("guanliyuan.jsp").forward(req, resp);
} else {
req.setAttribute("tip", "未知用户类型");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
} else {
req.setAttribute("tip", "用户名或密码错误");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
} catch (SQLException | ClassNotFoundException e) {
// 记录日志
e.printStackTrace();
throw new RuntimeException("数据库操作失败", e);
}
}
}
密码重置
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="org.example.kaoqin.Database" %>
<%--
Created by IntelliJ IDEA.
User: 32262
Date: 2024/12/9
Time: 19:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String jobid = request.getParameter("jobid");
try (
Connection con = Database.getConnection();
Statement st = con.createStatement();
ResultSet rs = jobid != null ? st.executeQuery("select * from zhiyuan where jobid = '" + jobid + "'") : st.executeQuery("select * from zhiyuan")) {
while (rs.next()) {
%>
<tr>
<td><%= rs.getString("jobid") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("sex") %></td>
<td><%= rs.getString("birthday") %></td>
<td><%= rs.getString("department") %></td>
<td><a href="resetpassword.jsp?jobid=<%= rs.getString("jobid") %>">重置密码</a></td>
</tr>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
<a href="guanliyuan.jsp">返回首页</a>
`
`<%--
Created by IntelliJ IDEA.
User: 32262
Date: 2024/12/9
Time: 19:56
To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="org.example.kaoqin.Database" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String jobid = request.getParameter("jobid");
String newPassword = "123456"; // 设置默认密码或从请求中获取新密码
try (Connection connection = Database.getConnection();
PreparedStatement statement = connection.prepareStatement("UPDATE zhiyuan SET password = ? WHERE jobid = ?")) {
statement.setString(1, newPassword);
statement.setString(2, jobid);
int rowsAffected = statement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("<h2>密码重置成功!</h2>");
} else {
System.out.println("<h2>密码重置失败!</h2>");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("<h2>发生错误!</h2>");
}
%>
返回查询页面
`
修改密码
`<%--
Created by IntelliJ IDEA.
User: 32262
Date: 2024/12/9
Time: 18:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
工号 | 姓名 | 性别 | 生日 | 部门 | 职务 |
---|