How to delete a row in a table using JSP. Error: NumberFormatException: null. -> Integer.parseInt(Unknown Source)

How to delete a row in a table using JSP. Error: NumberFormatException: null. -> Integer.parseInt(Unknown Source)

我正在使用 HSQLDB,试图让 CRUD 操作在 table 上运行。 删除功能有问题。 我不确定我是否正确传递了 'carId' 的值。

编辑:现在当我点击删除 link 时,会显示一个空白页面,没有任何错误显示。

index.jsp

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

CarDAO

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()

结束

继续上面的回答。而不是使用锚标签;我使用 type="submit" 的按钮或输入来解决问题。

<td>
    <form action="${pageContext.request.contextPath}/DeleteCarServlet?carId=${car.id}" method="post">
        <button type="submit" name="deleteBtn" class="btn btn-outline-primary" value="deleteBtn">Delete</button>
    </form>
</td>