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>