protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { long id = Utils.longParameterOrDefault(request.getParameter("id"), 0); ComputerAndCompanies queryResult = computerDatabaseService.computerByIdAndCompanies(id); ComputerForm form = new ComputerForm(queryResult.getComputer()); request.setAttribute("mode", "edit"); request.setAttribute("fieldValues", form.getFieldValues()); request.setAttribute("companies", queryResult.getCompanies()); request .getServletContext() .getRequestDispatcher("/WEB-INF/jsp/computer.jsp") .forward(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ComputerForm form = new ComputerForm( request.getParameter("name"), request.getParameter("introduced"), request.getParameter("discontinued"), request.getParameter("company")); if (form.isValid()) { Computer computer = form.getComputer(); long id = Utils.longParameterOrDefault(request.getParameter("id"), 0); computer.setId(id); boolean succesfull = computerDatabaseService.updateComputerAndSetCompany(computer, form.getCompanyId()); if (succesfull) { request .getSession() .setAttribute( "alertMessage", "Computer " + computer.getName() + " modified successfully"); } else { request .getSession() .setAttribute( "alertMessage", "There has been a problem while updating " + computer.getName()); } response.sendRedirect("../computers"); } else { request.setAttribute("mode", "edit"); request.setAttribute("companies", computerDatabaseService.allCompanies()); request.setAttribute("errorMessages", form.getErrorMessages()); request.setAttribute("fieldValues", form.getFieldValues()); request .getServletContext() .getRequestDispatcher("/WEB-INF/jsp/computer.jsp") .forward(request, response); } }