1

I made a shopping cart using servlet and jsp but i'm facing a problem which increments the quantity of cart item whenever I refresh the cart page. Actually what i'm doing is passing the particular product ID in the url whenever user click the add to cart button on some product just like addtocart.jsp?id=3 and then in according to the id I extract the product from the database and add it in the cart but when I refresh this URL addtocart?id=3 the whole procedure in this page run again which increments the product quantity everytime of a refresh. How can I solve this problem. Any suggestions?

product.jsp

<%
 String id = request.getParameter("id");
 Connection con = ConnectionManager.getConnection();
 PreparedStatement ps = con.prepareStatement("Select * from products inner join images using(product_name) "
         + "where product_id=?");
 ps.setString(1,id);
 ResultSet rs= ps.executeQuery();
 rs.next();

 String name = rs.getString("product_name");
 String image = rs.getString("image_name");
 String company = rs.getString("company_name");
 String category =  rs.getString("category_name");
 String sub = rs.getString("sub_category_name");
 double price = rs.getDouble("price");
 String summary = rs.getString("summary");
 int hits = rs.getInt("hits");

 //out.println(price);
%>
   <div class="container_16" style="background: #FFF;">
       <div class="grid_16 productHeading">
           <h2 class="heading"><%=name %>- By <%= company %> <%=category%> </h2>
       </div>

       <div class="grid_10">

             <h5>Category: <a href="#" onclick="return false"><%= category %> </a> >
              <a href="#" onclick="return false">  <%= sub %> </a><br/><br/>
              Priced At <span class="Bigred">RS. <%=price %></span> 
             </h5>

             <h2>Summary Of this Item</h2>


                       <div class="grid_5" id="addtocart">
                           <a href="addToCart.jsp?id=<%=id%>">
                              Add To Cart
                           </a>
                           <% if(session.getAttribute("admin")!=null)
                              {
                           %>   
                              <a href="addToCart.jsp?id=<%=id%>">
                              Edit  
                           </a>
                          <%              
                            }
                           %>   
                       </div>

               <br/>

             <h6 class="grey">Summary of <%= name%></h6>
                    <p class="info">
                        <%= summary%>
                    </p>       

       </div>  

       <div class="grid_4" id="pimage">
           <img src="<%= image%>">
       </div>    

  </div>    
palak sharma
  • 231
  • 1
  • 12

3 Answers3

2

Use Post/Redirect/Get Pattern:

  1. Submit form with POST method instead of GET
  2. Redirect back to cart page after processing post request.

Post/Redirect/Get flow https://en.wikipedia.org/wiki/Post/Redirect/Get

Konstantin Pavlov
  • 956
  • 1
  • 10
  • 24
0

Here are some possibilities to prevent this from happening.

  1. Change the form method to POST so that the browser asks the user if they want to resubmit the form if they ever refresh
  2. Redirect the form after it is accepted

Have a look at the answers to this question How to avoid resubmit in jsp when refresh?

After looking at your edited code, I would suggest adding code to remove the session variable after it is processed OR just use a hidden form and pass the parameters using the input tags

Community
  • 1
  • 1
Seda
  • 243
  • 1
  • 9
0

look at your code

  <a href="addToCart.jsp?id=<%=id%>">

use

        var url="addtocart.jsp";
        url +="?id=" +value;

and pass url as GET method

or

<a href="addToCart.jsp?id="<%=id%>>

not resolve then follow

How to pass the values from one jsp page to another jsp without submit button?

Community
  • 1
  • 1
Dheeraj Upadhyay
  • 336
  • 2
  • 12