Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @WebServlet(name = "CatalogController", urlPatterns = {"/item"})
- public class CatalogController extends HttpServlet {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- ItemDB itemDB = new ItemDB();
- List<Item> allItems = itemDB.getItems();
- String requestedItemCode = request.getParameter("itemCode");
- Item itsAllGoodBro = null;
- String url = "/item.jsp";
- for(Item item:allItems)
- {
- if(validateParameter(requestedItemCode))
- {
- if(requestedItemCode.equals(item.getItemCode()))
- {
- itsAllGoodBro = item;
- break;
- }
- }
- }
- if(itsAllGoodBro != null)
- {
- request.setAttribute("item", itsAllGoodBro);
- } else
- {
- url = "/categories.jsp";
- request.setAttribute("allItems", allItems);
- for (Item item : allItems) {
- request.setAttribute(item.getItemCode(), item);
- }
- }
- getServletContext().getRequestDispatcher(url).forward(request, response);
- }
- public boolean validateParameter(String requestedItemCode)
- {
- boolean validInput = true;
- if (requestedItemCode == null || requestedItemCode == "") {
- validInput = false;
- }
- return validInput;
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement