如何使用JSP删除表中的行。错误:NumberFormatException:null。 - > Integer.parseInt(未知来源)

问题描述 投票:0回答:2

我正在使用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>

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

>

java servlets hsqldb server-side dao
2个回答
1
投票

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

锚标签提出GET请求,而不是POST

您的请求最终在doGet()


0
投票

从上面的答案继续。而不是使用锚标签;我使用了一个按钮或输入类型=“提交”来解决问题。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.