Question

Hi I am trying to implement Add To Cart Mechanism in my web Application. So that's why temporary i have created 3 JSP pages that implement such mechanism. But it is not working properly.

I have also created session uniquely for to identify specific user session but it is not actually implemented in all page.

Following Is My Code :

test.jsp

<%
    Random rkey = new Random();
    int randomkey = Math.abs(rkey.nextInt());
    String sdata = "keyur"+randomkey;   

    DateFormat dateFormat = new SimpleDateFormat("HHmmss");
    Calendar cal = Calendar.getInstance();

    String sess = "keyur"+randomkey+dateFormat.format(cal.getTime());
    session.setAttribute("KEYUR", sess);
%>
<% response.sendRedirect("test1.jsp"); %>

test1.jsp

Hello <%= session.getAttribute("KEYUR")%>

<%
    String a="shirt",b="jeans";
    int a1 = 10,b1=20;
    Double a2=100.00,b2=200.00;
%>
<br><br>
A : <a href="test2.jsp?item=<%=a%>&qty=<%=a1%>&price=<%=a2%>" id="a1" name="a1">Add To Cart</a><br><br>
A1 : <a href="test2.jsp?item=<%=b%>&qty=<%=b1%>&price=<%=b2%>" id="a2" name="a2">Add To Cart</a>

test2.jsp

Hello <%= session.getAttribute("KEYUR")%><br><br><br><br>

Your Cart :
<%
    List<String> pname = new ArrayList<String>();
    List<Integer> pqty = new ArrayList<Integer>();
    List<Double> ppr = new ArrayList<Double>();
%>
<%
    pname.add(request.getParameter("item"));
    pqty.add(Integer.parseInt(request.getParameter("qty")));    
    ppr.add(Double.parseDouble(request.getParameter("price")));
    Double total=0.00;
%>
<br><br>
<%
    for(int i = 0;i < pname.size();i++)
    {
        String name = pname.get(i);
        Integer qty1 = pqty.get(i);
        Double pr1 = ppr.get(i);        
        %>
            Name : <%= name %><br>
            Qty : <%= qty1 %><br>
            Price : <%= pr1 %><br><br>
            Total : <%= total += qty1*pr1 %> 
        <%      
    }
%>

Right now i have taken static data, if it will work then I'll try it with dynamic data. Anyone tell me why only one item display in cart. Suppose user click on Add To Cart then it redirect to test2.jsp and again back to test1.jsp then click on second add to cart then cart should be appended not overwrite. But currently it is overwriting.

Any Suggestion Please...

Was it helpful?

Solution

test2.jsp

List<String> pname = new ArrayList<String>(); //size is 0

...

pname.add(request.getParameter("item")); //size is 1

That's why you get just one item.

By the way, make your self a favour, don't use JSP scriptlets; use JSTL or write your own tags (look for Java Custom tags)

OTHER TIPS

This procudure looking very Bad codding for store a cart items... But still you can store as many you want in List In your page when every time a Test2.jsp called the new keywprd initilize a new cart lists.. thats why previous value get losts.... instead of this code

<%
List<String> pname = new ArrayList<String>()
List<Integer> pqty = new ArrayList<Integer>();
List<Double> ppr = new ArrayList<Double>();
%>

use deceleration block for decleration

<%!
List<String> pname = new ArrayList<String>();
List<Integer> pqty = new ArrayList<Integer>();
List<Double> ppr = new ArrayList<Double>();
%>

after that you can store as many you want ....But there will be possible some duplicate values...to remove duplicate you can use Set at the place of List

I think you have got your query's answer.....

The reason why only one item is displayed is because you are inserting only one item in Lists pname, pqty, ppr.

Use a for loop in-order to insert. So that it contains multiple orders or items..

Hope it will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top