0

Beginner here. So here a sample of my code for you to stalk:

/// if addToCart action is called
if (userPath.equals("/addToCart")) {
    // if user is adding item to cart for first time
    // create cart object and attach it to user session
    if (cart == null) {
        cart = new ShoppingCart();
        session.setAttribute("cart", cart);
    }

    // get user input from request
    String productId = request.getParameter("productId");

    if (!productId.isEmpty()) {
        Product product = productFacade.find(Integer.parseInt(productId));
        cart.addItem(product);
    }
    userPath = "/category";
// if updateCart action is called
} else if (userPath.equals("/updateCart")) {
    // get input from request
    String productId = request.getParameter("productId");
    String quantity = request.getParameter("quantity");

    Product product = productFacade.find(Integer.parseInt(productId));
    cart.update(product, quantity);

    userPath = "/cart";
}

The IDE ignores the first value "/category" and assign "/cart" instead. This problem causes the application to behave strangely. For instance, when user clicks on add to cart in the browser, instead of staying on category page, it will go directly to the cart...

I need to use the unused value

... How do I fix this? I have tried few things around but without success so far... It is very strange indeed... First time I run into this issue...

EDIT

Some progress... Yesterday I spent an entire day trying to solve the issue which is still unsolved...

Could it be a bug that needs to be reported?

The fixes I tried so far:

  • Deleted the unused values... and retyped them which obviously did not do anything...
  • Played around with the brackets {} (worth a try)...
  • Changed the code a bit and called the values at the beginning of the class like this:

    @WebServlet(name = "Controller",
                loadOnStartup = 1,
                urlPatterns = {"/category",
                               "/addToCart",
    

    etc..

I'm thinking it has something to do with the statements not being closed somewhere? Since the one that gets assigned is the very last statement ignoring the ones before...

2nd EDIT on request

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
        Wishlist wishlist = (Wishlist) session.getAttribute("wishlist");

        /// if addToCart action is called
        if (userPath.equals("/addToCart")) {

            // if user is adding item to cart for first time
            // create cart object and attach it to user session
            if (cart == null) {

                cart = new ShoppingCart();
                session.setAttribute("cart", cart);
            }

            // get user input from request
            String productId = request.getParameter("productId");


            if (!productId.isEmpty()) {

                Product product = productFacade.find(Integer.parseInt(productId));
                cart.addItem(product);
            }

            userPath = "/category"; => UNUSED VALUE


       // if updateCart action is called
        } else if (userPath.equals("/updateCart")) {

            // get input from request
            String productId = request.getParameter("productId");
            String quantity = request.getParameter("quantity");

            Product product = productFacade.find(Integer.parseInt(productId));
            cart.update(product, quantity);

            userPath = "/cart"; => UNUSED VALUE

            // if addToWishlist action is called
        } else if (userPath.equals("/addToWishlist")) {

            // if user is adding item to wishlist for first time
            // create wishlist object and attach it to user session
            if (wishlist == null) {

                wishlist = new Wishlist();
                session.setAttribute("wishlist", wishlist);
            }

            // get user input from request
            String productId = request.getParameter("productId");


            if (!productId.isEmpty()) {

                Product product = productFacade.find(Integer.parseInt(productId));
                wishlist.addItem(product);
            }

            userPath = "/category"; => UNUSED VALUE

            // if updateWishlist action is called
        } else if (userPath.equals("/updateWishlist")) {

            // get input from request
            String productId = request.getParameter("productId");
            String quantity = request.getParameter("quantity");

            Product product = productFacade.find(Integer.parseInt(productId));
            wishlist.update(product, quantity);

            userPath = "/wishlist"; => UNUSED VALUE


        // if purchase action is called
        } else if (userPath.equals("/purchase")) {

        // extract user data from request
       String first_name = request.getParameter("first_name");
       String last_name = request.getParameter("last_name");
       String phone = request.getParameter("phone");
       String email = request.getParameter("email");
       String address_1 = request.getParameter("address_1");
       String address_2 = request.getParameter("address_2");
       String city = request.getParameter("city");
       String State_Province_Region = request.getParameter("State_Province_Region");
       String Postal_Zip_Code = request.getParameter("Postal_Zip_Code");
       String country = request.getParameter("Country");
            int orderId = 0;


        // save order to database

       // get order details
       Map orderMap = orderManager.getOrderDetails(orderId);

        // place order details in request scope
       request.setAttribute("customer", orderMap.get("customer"));
       request.setAttribute("products", orderMap.get("products"));
       request.setAttribute("orderRecord", orderMap.get("orderRecord"));
       request.setAttribute("orderedProducts", orderMap.get("orderedProducts"));

        }

        userPath = "/confirmation"; => USED VALUE FOR ALL OTHER ACTIONS BEFORE.

In other words, the IDE uses the last userPath which is /confirmation for all other userPath before him. So whether I click on /addToCart, /updateCart, /addToWishlist, /updateWishlist, the userPath that is assigned is the last userPath /confirmation which means everything is redirected to the confirmation page and it is not supposed to be this way. All other userPath are being ignored and set as unused value... I dont know how to fix this issue.

  • Which "unused value" are you talking about? What happens precisely when executing that code. if userPath becomes "/cart", then that means that userPath is equal to "/updateCart". But since we have no idea where this variable comes from... Please explain what you're doing, post all the relevant code, explain what you expect the code to do and what it does instead. If an exception is thrown as the title suggests, then post its full stack trace. If findbugs is relevant as the tags suggest, then post the complete message you get from FindBugs, and tell us what it complains about. – JB Nizet May 23 '15 at 05:51
  • Well, if you don't want userPath to have the value `"/confirmation"`, remove the line `userPath = "/confirmation";`. – JB Nizet May 24 '15 at 07:25

1 Answers1

0

Have a look at response.sendRedirect()

Monish Sen
  • 1,773
  • 3
  • 20
  • 32