jsp+eclipse+tomcat+mysql请假审批系统
时间: 2023-09-24 21:10:36 浏览: 228
请假审批系统是一个常见的办公自动化系统,下面是使用JSP+Servlet+Eclipse+Tomcat+MySQL实现的简单的请假审批系统的开发步骤:
1. 确定系统需求:请假申请、审批流程、数据存储等。
2. 设计数据库:创建请假表、用户表等。
3. 搭建开发环境:安装Eclipse、Tomcat、MySQL等。
4. 创建项目:在Eclipse中创建一个动态Web项目。
5. 编写代码:实现请假申请、审批、查询等功能。
6. 测试系统:在Tomcat上运行系统,进行测试和调试。
7. 部署系统:将系统部署到服务器上,供用户使用。
具体实现步骤如下:
1. 设计数据库
请假表(leave):
| 字段名 | 数据类型 | 描述 |
|------------|----------|------------|
| id | int | 请假编号 |
| user_id | int | 申请人编号 |
| start_time | datetime | 开始时间 |
| end_time | datetime | 结束时间 |
| reason | varchar | 请假事由 |
| status | int | 审批状态 |
用户表(user):
| 字段名 | 数据类型 | 描述 |
|---------|----------|----------|
| id | int | 用户编号 |
| name | varchar | 用户名 |
| account | varchar | 账号 |
| password| varchar | 密码 |
2. 搭建开发环境
1. 下载并安装Eclipse
2. 下载并安装Tomcat
3. 下载并安装MySQL
3. 创建项目
在Eclipse中创建一个动态Web项目,选择Tomcat作为服务器,选择JSP作为页面技术,选择MySQL作为数据库。
4. 编写代码
1. 创建数据库连接
```java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/leave_system?useUnicode=true&characterEncoding=utf8&useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "123456";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
```
2. 编写登录功能
```java
package servlet;
import dao.UserDAO;
import entity.User;
import util.DBUtil;
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.sql.Connection;
import java.sql.SQLException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String account = request.getParameter("account");
String password = request.getParameter("password");
try (Connection conn = DBUtil.getConnection()) {
User user = new UserDAO().getUser(conn, account, password);
if (user != null) {
request.getSession().setAttribute("user", user);
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("index.jsp?error=1");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
```
3. 编写请假申请功能
```java
package servlet;
import dao.LeaveDAO;
import entity.Leave;
import entity.User;
import util.DBUtil;
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.sql.Connection;
import java.sql.SQLException;
@WebServlet("/apply")
public class ApplyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
User user = (User) request.getSession().getAttribute("user");
String startTime = request.getParameter("start_time");
String endTime = request.getParameter("end_time");
String reason = request.getParameter("reason");
Leave leave = new Leave();
leave.setUserId(user.getId());
leave.setStartTime(startTime);
leave.setEndTime(endTime);
leave.setReason(reason);
try (Connection conn = DBUtil.getConnection()) {
new LeaveDAO().addLeave(conn, leave);
response.sendRedirect("main.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
```
4. 编写请假审批功能
```java
package servlet;
import dao.LeaveDAO;
import entity.Leave;
import util.DBUtil;
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.sql.Connection;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/approve")
public class ApproveServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int leaveId = Integer.parseInt(request.getParameter("leave_id"));
int status = Integer.parseInt(request.getParameter("status"));
try (Connection conn = DBUtil.getConnection()) {
new LeaveDAO().updateLeaveStatus(conn, leaveId, status);
response.sendRedirect("main.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int userId = Integer.parseInt(request.getParameter("user_id"));
try (Connection conn = DBUtil.getConnection()) {
List<Leave> list = new LeaveDAO().getLeavesByUserId(conn, userId);
request.setAttribute("list", list);
request.getRequestDispatcher("approve.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5. 编写查询请假记录功能
```java
package servlet;
import dao.LeaveDAO;
import entity.Leave;
import entity.User;
import util.DBUtil;
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.sql.Connection;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/query")
public class QueryServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int userId = Integer.parseInt(request.getParameter("user_id"));
try (Connection conn = DBUtil.getConnection()) {
List<Leave> list = new LeaveDAO().getLeavesByUserId(conn, userId);
request.setAttribute("list", list);
request.getRequestDispatcher("query.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
```
6. 测试系统
在Eclipse中启动Tomcat服务器,浏览器中访问http://localhost:8080/leave_system/index.jsp,输入账号密码进行登录,测试请假申请、审批、查询等功能。
7. 部署系统
将项目打包成.war文件,上传到服务器上,解压并部署到Tomcat服务器中。完成后,用户可以通过浏览器访问部署好的系统。
阅读全文