我正在使用HSQLDB,试图使CRUD操作在表上起作用。
删除功能出现问题。
我不确定我是否正确传递了'carId'的值。

编辑:现在,当我点击删除链接时,将显示一个空白页面,没有错误显示。


  index.jsp


<td>
      <c:if test="${sessionScope.user != null}">
      <a name="${car.id}"  href="DeleteCarServlet?carId=${car.id}"> Delete </a>
      </c:if>
</td>



  卡多


public Car deleteCar(int id) {

    Connection connection = Utils.getConnection();
    Car car = null;
    try {
        PreparedStatement psmt = connection
                .prepareStatement("DELETE FROM CAR WHERE ID = ?");
        psmt.setInt(1, id);
        psmt.executeUpdate();
        //list();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return car;
}



  DeleteCarServlet


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    int carId = Integer.parseInt(request.getParameter("carId"));
    Car car = CarDAO.instance.getById(carId);
    CarDAO.instance.deleteCar(carId);

    request.getRequestDispatcher("index.jsp").forward(request, response);


>

最佳答案

<a name="${car.id}" href="DeleteCarServlet?carId=${car.id}"> Delete </a>

定位标记发出GET请求,而不是POST

您的请求以doGet()结尾

10-05 19:42