Maciek Maćkowiak
Maciek Maćkowiak

Reputation: 1

Servlet doesn't respond properly after sending request

so i have this simple website in jsp that contains todo list, i've already made Add functionality but now there's a problem with delete function.

  <ol class="list-group">
            <c:forEach items="${todos}" var="todo">
                <li class="list-group-item">${todo.toDoPosition} &nbsp; <a class="btn btn-light" href="${pageContext.request.contextPath}/delete-todo.do?uuidDelete=${todo.uuid}">Delete</a></li>
            </c:forEach>
        </ol>

Every toDoPosition has unique UUID and delete function should delete position by comparing toDoPosition uuid with this one send in parameter, i've already checked if these two uuid are same and it is but it still doesn't want to delete position from the list.

There is a servlet that supports Delete function

private TodoService todoService = new TodoService();


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(request.getParameter("uuidDelete") + "Should be deleted now");
    todoService.deleteTodo(new Todo(request.getParameter("uuidDelete")));
    response.sendRedirect("/todo.do");

}

It redirect to servlet that has a method returnList() in doGet and it also has doPost method which responds for adding new todo:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getSession().setAttribute("todos", todoService.returnList() );
    request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response);
}
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getParameter("newTodo") != null) {
        todoService.addTodo(new Todo(request.getParameter("newTodo")));
    }
    response.sendRedirect("/todo.do");
}

And a delete service:

    protected List<Todo> toDoList = new ArrayList<>();


public List<Todo> returnList(){
    return toDoList;
}

public void addTodo(Todo toDo) {
    toDoList.add(toDo);
    System.out.println(toDo.uuid);
}

public void deleteTodo(Todo toDo) {
    if(toDoList.contains(toDo.uuid)) {
        toDoList.remove(toDo);
    }

}

After clicking Delete it sends request with UUID, website refresh but there is no change in list. It seems like delete method doesn't remove position from the list because after i redirect it to /todo.do and print list position that should be deleted is still here and i have no idea why

Upvotes: 0

Views: 78

Answers (1)

NinePlanFailed
NinePlanFailed

Reputation: 395

In deleteTodo you pass in a Todo object. You then use the passed in Todo uuid attribute in the List contains method, which is incorrect. You need to pass the Todo object into the contains method, not just the ID for it, as the list contains method and compares the objects, not their uuid attribute. If you debug this code you should see the List remove method is never called. You may also need to override equals in your Todo object for this to work, making equals do a compare of the uuid attribute.

Upvotes: 1

Related Questions