英文:
I can not get updated data on the page after Update request to the database (data updated only after restart applications)
问题
@Web服务("/cancel")
public class CancelReservationServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest请求,HttpServletResponse resp) throws ServletException,IOException,NumberFormatException {
String reservId = req.getParameter("id");
if (reservId.isEmpty()) {
resp.sendRedirect(req.getContextPath() + "/cancel");
} else {
ReservationService reservationService = new ReservationService();
尝试 {
reservationService.stopReservation(Integer.parseInt(reservId));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
resp.sendRedirect(req.getContextPath() + "/allreservs");
}
}
}
@Web服务("/allreservs")
public class AllReservationsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ReservationService reservationService = new ReservationService();
Set<ReservationDto> reservations = null;
尝试 {
reservations = reservationService.getListOfReserves();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("reservations", reservations);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/pages/allreservs.jsp");
requestDispatcher.forward(req, resp);
}
}
停止服务类中的停止方法
public void stopReservation(Integer reservationId) throws IOException, ClassNotFoundException, SQLException {
Connection connection = ConnectionFactory.getConnection();
尝试 {
preparedStatement = connection.prepareStatement("UPDATE reservation set isActive = false where reservationId = ?");
preparedStatement.setInt(1, reservationId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
JSP部分
<%@ 标签库前缀="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>所有预订</title>
<link href="https://fonts.googleapis.com/css2?family=Baloo+Tamma+2:wght@500;600;700&display=swap" rel="stylesheet">
<style>
<%@include file="/styles/style.css"%>
</style>
</head>
<body>
<p>
<a href='<c:url value="/"/>'><- 主页</a>
</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>全名</th>
<th>操作</th>
<th>描述</th>
<th>开始时间</th>
<th>结束时间</th>
<th>是否活动</th>
<th>房间号</th>
<th colspan="2">取消</th>
</tr>
</thead>
<c:forEach items="${reservations}" var="reservations">
<tbody>
<tr>
<td>${reservations.id}</td>
<td>${reservations.fullName}</td>
<td>${reservations.manipulationName}</td>
<td>${reservations.description}</td>
<td>${reservations.startTime}</td>
<td>${reservations.endTime}</td>
<td>${reservations.isActive}</td>
<td>${reservations.roomNumber}</td>
<td><form action="${pageContext.request.contextPath}/cancel" method="post">
<td>
<button onclick="location.href='/cancel'">取消</button>
<input type="hidden" name="id" value="${reservations.id}">
</td>
</form></td>
</tr>
</tbody>
</c:forEach>
</table>
</body>
</html>
附加部分
public Set<ReservationDto> getListOfReserves() throws IOException, ClassNotFoundException, SQLException {
Connection connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(GET_RESERVE_DATA);
result = preparedStatement.executeQuery();
while (result.next()) {
reservs.add(new ReservationDto(
result.getInt("reservationId"),
result.getString("fullName"),
result.getString("manipulationName"),
result.getString("description"),
result.getTimestamp("startTime"),
result.getTimestamp("endTime"),
result.getBoolean("isActive"),
result.getInt("roomNumber")));
}
return reservs;
}
private static final String GET_RESERVE_DATA = "SELECT rsrv.reservationId, CONCAT(empl.name, ' ', empl.surname) as fullname, " +
"rsrv.manipulationName, rsrv.description, rsrv.startTime, " +
"rsrv.endTime, rsrv.isActive, r.roomNumber, empl.employeeId, r.roomId FROM reservation AS rsrv " +
"JOIN room as r ON rsrv.roomid = r.roomId " +
"JOIN employee as empl ON rsrv.emplId = empl.employeeId ";
英文:
I trying to stop current reservations, by clicking on CANCEL button, which call stopReservation method. Data updated in database, but redirect return the same list as was, without chages. Only after restart I get updatetd list with data. Network show 302 to cancel and 200 to allreservs, but there no chages in list.
@WebServlet("/cancel")
public class CancelReservationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/pages/cancel.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NumberFormatException {
String reservId = req.getParameter("id");
if (reservId.isEmpty()) {
resp.sendRedirect(req.getContextPath() + "/cancel");
} else {
ReservationService reservationService = new ReservationService();
try {
reservationService.stopReservation(Integer.parseInt(reservId));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
resp.sendRedirect(req.getContextPath() + "/allreservs");
}
}
}
@WebServlet("/allreservs")
public class AllReservationsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ReservationService reservationService = new ReservationService();
Set<ReservationDto> reservations = null;
try {
reservations = reservationService.getListOfReserves();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("reservations", reservations);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/pages/allreservs.jsp");
requestDispatcher.forward(req, resp);
}
}
STOP METHOD FROM SERVICE CLASS
public void stopReservation(Integer reservationId) throws IOException, ClassNotFoundException, SQLException {
Connection connection = ConnectionFactory.getConnection();
try {
preparedStatement = connection.prepareStatement("UPDATE reservation set isActive = false where reservationId = ?");
preparedStatement.setInt(1, reservationId);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
JSP
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>All reservation</title>
<link href="https://fonts.googleapis.com/css2?family=Baloo+Tamma+2:wght@500;600;700&display=swap" rel="stylesheet">
<style>
<%@include file="/styles/style.css"%>
</style>
</head>
<body>
<p>
<a href='<c:url value="/"/>'><- main page</a>
</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>FULL NAME</th>
<th>MANIPULATION</th>
<th>DESCRIPTION</th>
<th>START TIME</th>
<th>END TIME</th>
<th>IS ACTIVE</th>
<th>ROOM NUMBER</th>
<th colspan="2"> CANCEL </th>
</tr>
</thead>
<c:forEach items="${reservations}" var="reservations">
<tbody>
<tr>
<td>${reservations.id}</td>
<td>${reservations.fullName}</td>
<td>${reservations.manipulationName}</td>
<td>${reservations.description}</td>
<td>${reservations.startTime}</td>
<td>${reservations.endTime}</td>
<td>${reservations.isActive}</td>
<td>${reservations.roomNumber}</td>
<td><form action="${pageContext.request.contextPath}/cancel" method="post">
<td>
<button onclick="location.href='/cancel'">cancel</button>
<input type="hidden" name="id" value="${reservations.id}">
</td>
</form></td>
</tr>
</tbody>
</c:forEach>
</table>
</body>
</html>
additional:
public Set<ReservationDto> getListOfReserves() throws IOException, ClassNotFoundException, SQLException {
Connection connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(GET_RESERVE_DATA);
result = preparedStatement.executeQuery();
while (result.next()) {
reservs.add(new ReservationDto(
result.getInt("reservationId"),
result.getString("fullName"),
result.getString("manipulationName"),
result.getString("description"),
result.getTimestamp("startTime"),
result.getTimestamp("endTime"),
result.getBoolean("isActive"),
result.getInt("roomNumber")));
}
return reservs;
}
private static final String GET_RESERVE_DATA = "SELECT rsrv.reservationId, CONCAT(empl.name, ' ', empl.surname) as fullname, " +
"rsrv.manipulationName, rsrv.description, rsrv.startTime, " +
"rsrv.endTime, rsrv.isActive, r.roomNumber, empl.employeeId, r.roomId FROM reservation AS rsrv " +
"JOIN room as r ON rsrv.roomid = r.roomId " +
"JOIN employee as empl ON rsrv.emplId = empl.employeeId ";
答案1
得分: 0
在函数内部声明 reservs
集合。因为看起来在这个集合中有之前的值,这是因为你必须已经在函数外部声明了它。如果你不想要这样,你可以在添加新值之前清空集合。
方法1
在 getListOfReserves()
函数内部的声明中添加 Set<ReservationDto> reservs = new HashSet<>();
方法2
在遍历结果之前,使用 reservs.clear()
来从集合中移除所有先前的值。
英文:
Declare the reservs
set inside the function. As it seems that their are previous values in this set, since you must have declared it outside the function. If you don't want that, you can clear the set before adding any new values to it.
Method 1
Inside getListOfReserves()
declare Set<ReservationDto> reservs = new HashSet<>();
Method 2
Just before iterating the result. User clear to remove all the previous values from the set. reservs.clear()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论