• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Checking the size of an ArrayList in a JSP page

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner to Java. I am doing an assignment of a Shopping cart application. Below is the action class I have created. I want a number to display in the JSP page each time the size of the array increases. How do I call the arraylist from the servlet in the JSP page? Any help would be greatly appreciated. Thank You.

public class ItemAddToCartAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();
try {
Hashtable item = new Hashtable();
item.put("brand", request.getParameter("brand"));
item.put("color", request.getParameter("color"));
item.put("size", request.getParameter("size"));
item.put("price", request.getParameter("price"));
item.put("shipping", request.getParameter("shipping"));
if (session.getAttribute("cart") == null) {
ArrayList cart = new ArrayList();
cart.add(item);
session.setAttribute("cart", cart);
}

else {
ArrayList cartFromSession = (ArrayList)
session.getAttribute("cart");
cartFromSession.add(item);
session.setAttribute("cart",cartFromSession);
}

} catch (Exception e) {
System.out.println("Exception occured");
return mapping.findForward("Failure");
}
return mapping.findForward("SUCCESS");

}
}
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"SDN",

Welcome to JavaRanch. We don't have many rules here, but we do have a naming policy which we try to strictly enforce. Please re-read this document and edit your display name in order to comply. Thanks in advance, and we look forward to seeing you around the Ranch.
 
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are many ways to do this. here's one using scriptlets inside your JSP:

<% HttpSession session = request.getSession();
int itemsCount = ((ArrayList)request.getAttribute("cart")).size(); %>

<%=itemsCount%>
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alan,
It should be session.getAttribute and not request
Also adding to your point we dont even need to use request.getSession()
we can use session implicit object directly.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by SDN:
I am a beginner to Java. I am doing an assignment of a Shopping cart application. Below is the action class I have created. I want a number to display in the JSP page each time the size of the array increases. How do I call the arraylist from the servlet in the JSP page? Any help would be greatly appreciated. Thank You.

public class ItemAddToCartAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();
try {
Hashtable item = new Hashtable();
item.put("brand", request.getParameter("brand"));
item.put("color", request.getParameter("color"));
item.put("size", request.getParameter("size"));
item.put("price", request.getParameter("price"));
item.put("shipping", request.getParameter("shipping"));
if (session.getAttribute("cart") == null) {
ArrayList cart = new ArrayList();
cart.add(item);
session.setAttribute("cart", cart);
}

else {
ArrayList cartFromSession = (ArrayList)
session.getAttribute("cart");
cartFromSession.add(item);
session.setAttribute("cart",cartFromSession);
}

} catch (Exception e) {
System.out.println("Exception occured");
return mapping.findForward("Failure");
}
return mapping.findForward("SUCCESS");

}
}

 
Cendy Nguvy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add this at the top of your jsp, before tag <html> :

<jsp:useBean id="cart" scope="session" type="java.util.ArrayList"/>

and in tag <body> :

<%=cart.size()%>
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic