Java Servlet list.remove不起作用

时间:2018-08-04 22:51:07

标签: java list servlets

我有一个字符串列表,list.add可以正常工作,但是list.remove不起作用。这是我的代码:

公共类CartServlet扩展了HttpServlet {     私有静态最终长serialVersionUID = 1L;

public List<String> cart = new ArrayList<String>();
public CartServlet() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //cart.forEach(System.out::println);
    request.setAttribute("cart",cart);
    request.getRequestDispatcher("WEB-INF/Cart.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String productAdd = request.getParameter("add");
    String productDelete = request.getParameter("delete");
    if(productAdd != null) {
        cart.add(productAdd);
        //System.out.println(cart.indexOf(productAdd));

    }
    else {
        cart.remove(productDelete);//this is not working
    }
    doGet(request, response);
}

}

我这样添加: add item

然后我的购物车是: cart 我用一个按钮删除了相同的方法。但是由于某种原因,我正确地获得了productDelete,但是cart.remove(productDelet)不起作用,并且字符串在那里,我感觉字符串不匹配。

0 个答案:

没有答案