/** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub int bookID = Integer.valueOf(request.getParameter("bID")); BookDAO bookDao = new BookDAO(); List<Book> allBooks = bookDao.getAll(); for (Book book : allBooks) { if (book.getId() == bookID) { book.setStatus("not active"); bookDao.delete(book); } } RequestDispatcher rq = request.getRequestDispatcher("LoginBookPreviewViewController"); rq.forward(request, response); }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String message = "File upload."; List<String> filenames = null; isMultipart = ServletFileUpload.isMultipartContent(request); HttpSession session = request.getSession(false); User loggedUser = null; if (session != null) loggedUser = (User) session.getAttribute("loggedUser"); if (loggedUser == null) { request.getRequestDispatcher("login.jsp").forward(request, response); } else { Book book = new Book(); if (isMultipart) { ServletContext context = this.getServletConfig().getServletContext(); DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. File repoPath = (File) context.getAttribute("javax.servlet.context.tempdir"); factory.setRepository(repoPath); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { List<FileItem> fileItems = upload.parseRequest(new ServletRequestContext(request)); filePath = request.getServletContext().getRealPath("/") + "picture/"; filenames = new ArrayList<String>(); for (FileItem fi : fileItems) { if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); if (fi.getName() != null && !fi.getName().equals("")) { String fileName = URLEncoder.encode(fi.getName(), "UTF-8"); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); file = new File(filePath + fileName); fi.write(file); book.setPictureURL("picture/" + fileName); } } else { String fieldName = fi.getFieldName(); String fieldValue = fi.getString(); if (fieldName.equals("title")) { if (fieldValue != null) { book.setTitle(fieldValue); ; } } if (fieldName.equals("authors")) book.setAuthors(fieldValue); if (fieldName.equals("publisher")) book.setPublisher(fieldValue); if (fieldName.equals("description")) if (fieldValue != null) { book.setDescription(fieldValue); } if (fieldName.equals("genre")) book.setGenre(fieldValue); if (fieldName.equals("numberInStock")) book.setNumberInStock(Integer.parseInt(fieldValue)); if (fieldName.equals("publicationYear")) book.setPublicationYear(Integer.parseInt(fieldValue)); if (fieldName.equals("price")) book.setPrice(Double.parseDouble(fieldValue)); } } } catch (Exception ex) { System.out.println(ex); } } BookDAO bookDao = new BookDAO(); bookDao.add(book); response.sendRedirect("LoginBookPreviewViewController"); } }