Thursday, 15 May 2014

jsp - Java Servlet if statment don't need { } brackets? -


I'm starting a new one and reading through Murach's Java Servlet and JSP ... through examples This CH 11 is being trapped in simple shopping cart examples.
I'll post the whole code here, but its long way is long.

I have put the complete code on my Dropbox link:

Question:

  1. CartServlet.java

      if (quantity> 0) cart.addItem (lineItem); And if (quantity == 0) cart.removeItem (lineItem); Session.setAttribute ("cart", cart); String url = "/ cart.jsp"; Request Dispatcher Dispatcher = getServletContext (). GetRequestDispatcher (url); Dispatcher. Forwarded (request, response);   

    This is probably a dumb question I notice for the servlet if the statement does not require you {}? Why is it like this? I mean that if regular JAVA statement requires all {} then why is the servicelet different?

  2. cart.java

      public zero addItem (LineItem item) {string code = item.getProduct (). GetCode (); Int Volume = Item Metatata (); For (int i = 0; i and lt; madasize (); i ++) {linelight line item = items.jet (i); If (lineItem.getProduct (). GetCode (.) Equal (code)) {lineItem.setQuantity (quantity); Return; }} Items.add (item); }     

    What I can not follow is item.getProduct (). GetCode (); I'm not really sure what this output will be. 1) Servletets are also Java code and rules applicable to Core Java also apply to the Serot {} The number of statements in a block is used to combine the former.

      if (condition) statement 1; Statement 2;   

    In the above example 1, only the execution will be done only when the situation is correct. If the statement of status 2 if is not made, it will be executed as a general statement.

      if (condition) {statement 1; Statement 2; }   

    Now in the above example, as you have attached Statement 1 and the entire block in {} will be a part of if and both The statement will be executed only when if (condition) evaluates for the correct.

    2) if (lineItem.getProduct). GetCode () Par value (code)

    In the above statement lineItem.getProduct () return product object. Then lineItem.getProduct () becomes product.getCode (). Equal (code) . Now returns the product.getCode () code object. Then product.getCode (). The code becomes code.equals (code) and if both are equal, then if evaluates the correct.

No comments:

Post a Comment