Package org.geekhub.dt

Source Code of org.geekhub.dt.DeleteItemServlet

package org.geekhub.dt;

import org.geekhub.dt.objects.Animal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.TreeMap;

@WebServlet(urlPatterns = "/delete")
public class DeleteItemServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        TreeMap<String, List<Animal>> map = (TreeMap<String, List<Animal>>) session.getAttribute("map");

        int id = Integer.parseInt(request.getParameter("id"));
        Animal animalToDelete = null;
        for (List<Animal> animals : map.values()) {
            for (Animal animal : animals) {
                if (animal.getId() == id) {
                    animalToDelete = animal;
                }
            }

            if (animalToDelete != null) {
                animals.remove(animalToDelete);
                animalToDelete = null;
            }
        }
        session.setAttribute("map", map);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
TOP

Related Classes of org.geekhub.dt.DeleteItemServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.