编写一个简单的机房预约系统,通过该系统实验室管理员可以完成机房的预约登记操作; 编写展示WelcomeServlet,该Servlet映射到首地址“/”,通过Java Bean获取所有机房的预约信息,并将数据转发到reservation .jsp页面;
时间: 2024-05-11 08:18:27 浏览: 125
毕业设计:Java项目之jsp实验室预约管理系统(源码 + 数据库 + 说明文档)
机房预约系统代码:
```
public class RoomReservation {
private int id; // 预约编号
private String roomName; // 机房名称
private String date; // 预约日期
private String timeSlot; // 预约时间段
private String applicant; // 申请人
// 构造方法
public RoomReservation(int id, String roomName, String date, String timeSlot, String applicant) {
this.id = id;
this.roomName = roomName;
this.date = date;
this.timeSlot = timeSlot;
this.applicant = applicant;
}
// setter and getter 略
@Override
public String toString() {
return "机房:" + roomName + ",日期:" + date + ",时间段:" + timeSlot + ",申请人:" + applicant;
}
}
public class RoomReservationSystem {
private List<RoomReservation> reservations; // 预约列表
// 构造方法
public RoomReservationSystem() {
reservations = new ArrayList<>();
}
// 添加预约
public void addReservation(RoomReservation reservation) {
reservations.add(reservation);
}
// 获取所有预约信息
public List<RoomReservation> getAllReservations() {
return reservations;
}
// 按机房名称查找预约信息
public List<RoomReservation> getReservationsByRoomName(String roomName) {
List<RoomReservation> result = new ArrayList<>();
for (RoomReservation reservation : reservations) {
if (reservation.getRoomName().equals(roomName)) {
result.add(reservation);
}
}
return result;
}
}
public class RoomReservationServlet extends HttpServlet {
private RoomReservationSystem system; // 预约系统
public void init() {
system = new RoomReservationSystem();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取参数
int id = Integer.parseInt(request.getParameter("id"));
String roomName = request.getParameter("roomName");
String date = request.getParameter("date");
String timeSlot = request.getParameter("timeSlot");
String applicant = request.getParameter("applicant");
// 创建预约
RoomReservation reservation = new RoomReservation(id, roomName, date, timeSlot, applicant);
// 添加预约
system.addReservation(reservation);
// 返回结果
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("预约成功!");
out.close();
}
}
```
WelcomeServlet代码:
```
public class WelcomeServlet extends HttpServlet {
private RoomReservationSystem system; // 预约系统
public void init() {
system = new RoomReservationSystem();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取所有预约信息
List<RoomReservation> reservations = system.getAllReservations();
// 将预约信息存入Java Bean
ReservationBean bean = new ReservationBean();
bean.setReservations(reservations);
// 转发到reservation.jsp页面
request.setAttribute("bean", bean);
RequestDispatcher dispatcher = request.getRequestDispatcher("/reservation.jsp");
dispatcher.forward(request, response);
}
}
public class ReservationBean {
private List<RoomReservation> reservations; // 预约列表
// setter and getter 略
}
```
reservation.jsp页面代码:
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>机房预约信息</title>
</head>
<body>
<table border="1">
<tr>
<td>预约编号</td>
<td>机房名称</td>
<td>预约日期</td>
<td>预约时间段</td>
<td>申请人</td>
</tr>
<c:forEach items="${bean.reservations}" var="reservation">
<tr>
<td>${reservation.id}</td>
<td>${reservation.roomName}</td>
<td>${reservation.date}</td>
<td>${reservation.timeSlot}</td>
<td>${reservation.applicant}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
阅读全文