Good evening,

I have a simple problem. I'm making a shopping basket and i want to add some prices for the items but i can not figure out how to pass the value from one page to another in order to view the total ammount of payment in the cart view.
For example, i want a cpu to cost 200 and a vga 300. When someone goes to cart, there should be a total 500.

Here is the code so far:

add.jsp
<center>
    <html>
        <head>
            <title>My Shopping Cart</title>
            <style>
                *{
                    font-size:12px; 
                    font-family:Verdana; 
                }
                input
                { 
                    border:1px solid #cccccc
                }
            </style>
        </head>
        <body>
            <!--declaring variables here-->
            <jsp:declaration>
                java.util.Enumeration parms;
                java.util.Enumeration values;
            </jsp:declaration>

            <!--put variables into session-->
            <jsp:scriptlet>
                parms = request.getParameterNames();
                values = request.getParameterNames();

                while(parms.hasMoreElements()) 
                {
                    String name = (String) parms.nextElement();
                    String value = (String) values.nextElement();
                    session.setAttribute(name, value);
                }
            </jsp:scriptlet>

            <!--references-->
            <img src="images/add.png" onclick="document.location='add.jsp'">
            <img src="images/remove.png" onclick="document.location='remove.jsp'">
            <img src="images/cart.png" onclick="document.location='cart.jsp'">

            <!--header-->
            <h2>Check to add items</h2>

            <form method="get" action="add.jsp">
                <table>
                    <tr>
                        <td>
                            <input type="checkbox" <% if (session.getAttribute("cpu") != null) 
                            out.print("checked"); %> name="cpu">
                        </td>
                        <td>cpu</td>
                    </tr>

                    <tr>
                        <td>
                            <input type="checkbox" <% if (session.getAttribute("vga") != null)
                            out.print("checked"); %> name="vga">
                        </td>
                        <td>vga</td>
                    </tr>

                    <tr>
                        <td>
                            <input type="checkbox" <% if (session.getAttribute("motherboard") != null)
                            out.print("checked"); %> name="motherboard">
                        </td>
                        <td>motherboard</td>
                    </tr>

                    <tr>
                        <td>
                            <input type="checkbox" <% if (session.getAttribute("ssd") != null)
                            out.print("checked"); %> name="ssd">
                        </td>
                        <td>ssd</td>
                    </tr>

                    <tr>
                        <td>
                            <input type="checkbox" <% if (session.getAttribute("case") != null)
                            out.print("checked"); %> name="case">
                        </td>
                        <td>case</td>
                    </tr>
                </table>

                <br/><br/>

                <input type="submit" value="submit">
            </form>
        </body>
    </html>
</center>


remove.jsp
<center>
    <html>
        <head>
            <title>My Shopping Cart</title>
            <style>
                *{ 
                    font-size:12px; 
                    font-family: Verdana; 
                }
                input 
                { 
                    border:1px solid #cccccc 
                }
            </style>
        </head>

        <body>
            <jsp:declaration>
                java.util.Enumeration parms;
            </jsp:declaration>

            <jsp:scriptlet>
                parms = request.getParameterNames();

                while(parms.hasMoreElements()) 
                {
                    String name = (String) parms.nextElement();
                    session.removeAttribute(name);
                }
            </jsp:scriptlet>


            <img src="images/add.png" onclick="document.location='add.jsp'">
            <img src="images/remove.png" onclick="document.location='remove.jsp'">
            <img src="images/cart.png" onclick="document.location='cart.jsp'">

            <h2>Check to remove items</h2>

            <form method="get" action="remove.jsp">

                <table>
                    <% if (session.getAttribute("cpu") != null) { %>
                        <tr>
                            <td><input type="checkbox" name="cpu"></td><td>cpu</td></td>
                        </tr>
                    <% } %>

                    <% if (session.getAttribute("vga") != null) { %>
                    <tr>
                        <td><input type="checkbox" name="vga"></td><td>vga</td></td>
                    </tr>
                    <% } %>

                    <% if (session.getAttribute("motherboard") != null) { %>
                    <tr>
                        <td><input type="checkbox" name="motherboard"></td><td>motherboard</td></td>
                    </tr>
                    <% } %>

                    <% if (session.getAttribute("ssd") != null) { %>
                    <tr>
                        <td><input type="checkbox" name="ssd"></td><td>ssd</td></td>
                    </tr>
                    <% } %>

                    <% if (session.getAttribute("case") != null) { %>
                    <tr>
                        <td><input type="checkbox" name="case"></td><td>case</td></td>
                    </tr>
                    <% } %>
                </table>

                <br/><br/>

                <input type="submit" value="submit">
            </form>
        </body>
    </html>
</center>


cart.jsp
<center>
    <html>
        <head>
            <title>My Shopping Cart</title>
            <style>
                *{
                    font-size:12px;
                    font-family:Verdana; 
                }
            </style>
        </head>

        <body>
            <img src="images/add.png" onclick="document.location='add.jsp'">
            <img src="images/remove.png" onclick="document.location='remove.jsp'">
            <img src="images/cart.png" onclick="document.location='cart.jsp'">

            <h2>Shopping Cart Status:</h2>

            <jsp:scriptlet>
                <![CDATA[ 
                    java.util.Enumeration content = session.getAttributeNames();
                    while (content.hasMoreElements()) 
                    {
                        out.println(content.nextElement());
                        out.println("<br>");
                    }
                 ]]>
            </jsp:scriptlet>
        </body>
    </html>
</center>

how about a javaBean or a global variable

<jsp:setProperty 
        name="beanInstanceName" 
        {   
            property= "*"   | 
            property="propertyName" [ param="parameterName" ]   | 
            property="propertyName" value="{string | <%= expression %>}" 
        } 
/>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.