1

I currently have the following:

cartServlet.java

public class CartServlet extends HttpServlet{

private static final long serialVersionUID = 1L;

CartBean cartBean = new CartBean();

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

    int counter = 0;

    while (request.getParameterMap().containsKey("id" + counter)){
        String songID = request.getParameter("id" + counter);
        cartBean.setCartInfo(songID);               
        ++counter;
    }


    request.setAttribute("cart", cartBean);


    RequestDispatcher rd = request.getRequestDispatcher("/cart.jsp");
    rd.forward(request, response);


}

cart.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Shopping Cart</title>
</head>
<body>

"${cart.cartInfo}"

</body>
</html>

cartBean.java

public class CartBean implements Serializable {

private static final long serialVersionUID = 1L;

private List<String> cart;

public CartBean(){
    cart = new ArrayList<String>();     
}

public void setCartInfo(String cartItem) {
    this.cart.add(cartItem);
}   

public List<String> getCartInfo() {
    return cart;
}   

}

When I print "${cart.cartInfo}", my output is coming out like this:

"[381d3af3-c113-46c1-b9d0-2c46cf445e22}, 3913ac54-0c03-4025-8279-5cfad2fcab5f}, 50ed6861-f6e2-479b-865c-cbbbc5c27efd}, eb9b29d6-d93e-4cd8-8d7a-7fe26ff6c05d}]"

Is this the correct way the output should be printed out? I don't know why the additional } is appearing at the end of each item..

Also, should I be defining CartBean cartBean = new CartBean(); in cartServlet.java? If a user were to come back to this shopping cart page and select more items, would the new items be placed in a different bean to the one I was originally using?

Thanks for your help.

2 Answers2

0

Is this the correct way the output should be printed out? I don't know why the additional } is appearing at the end of each item..

I'm guessing that yes, what's being listed on your JSP is what's supposed to be listed. Now it may differ from what you want, which is another matter. What appears to be currently listed is the ID for a song. In this XML file, I see one of the IDs listed as for "The Sweetest Taboo" by Sade in this homework file: http://www.cse.unsw.edu.au/~cs9321/14s1/assignments/musicDb.xml.

You need to use the useBean tag in your JSP. The syntax is as follows:

<jsp:useBean id = "idName" class = "packageName.SpecificClass" scope = "desiredScope" />

Fill in id, class, and scope with the desired values. The most common value for scope seems to be session.

Then set the property:

<jsp:setProperty name = "idName" property = "*" />

For more information about setProperty (along with useBean), see: jsp:setproperty what does property="*" mean?.

Also, should I be defining CartBean cartBean = new CartBean(); in cartServlet.java?

It's usually best to use the servlet for the bean, especially if there's some processing that's going on, like filling in lists. Avoid putting a lot of Java code in your JSPs. The JSP should be able to grab a list of products and place them on the page, but it should not be able to instantiate the list, populate it, massage it into the form you need, and then place the products.

If a user were to come back to this shopping cart page and select more items, would the new items be placed in a different bean to the one I was originally using?

Read this page: http://www.jguru.com/faq/view.jsp?EID=53309.

Community
  • 1
  • 1
TMBT
  • 1,183
  • 10
  • 17
0

You need to put the statement of creating CartBean inside doPost() method as a local variable, otherwise each request will create a new Thread and these threads share the instance variable which means the data in CartBean will be corrupted by different users.

sendon1982
  • 9,982
  • 61
  • 44