0

I'm new to Servlet and I have a simple shopping cart that add the details of the product.

When I submit the form to Cart its show an empty page.

I have a form from product.jsp

<form action="Cart" method="post">
    <div class="col-xs-2">
     <input type="number" class="form-control" size="50" name="Qty" placeholder="Quantity" min="30" max="99999" required/>
     <input type="hidden" value="<%=rs.getString("price")%>" name="price" />
     <input type="hidden" value="<%=rs.getString("productName")%>" name="prodname" />
     <input type="hidden" value="<%=rs.getString("ID")%>" name="ID" />

    </div>
    <strong style="float: left;"> Between 30 and 99,999 pieces.</strong>
    <input type="submit" class="btn btn-default" value="Add to Cart"/>
</form>

This is my Servlet Cart.java

ID = Integer.parseInt(request.getParameter("ID"));
price = Double.parseDouble(request.getParameter("price"));
prodname = request.getParameter("prodname");
qty = Integer.parseInt(request.getParameter("Qty"));
tprice = price * qty;
      
      
list = (ArrayList) request.getSession().getAttribute("listt");
if(list == null){
 list = new ArrayList();
 }
  
// Cart1.java with Getter and Setter
Cart1 s = new Cart1(ID, qty, prodname, price, tprice);

      
list.add(s);
      
request.getSession().setAttribute("listt", list);
      
RequestDispatcher rd = request.getRequestDispatcher("/Cart.jsp");
rd.forward(request, response);      

and this is my Cart.jsp

<table align="center" border="1px">
  <% ArrayList list = (ArrayList) request.getSession().getAttribute("listt"); %>
  <% Iterator itr = list.iterator(); %>
  <% while(itr.hasNext()) { %>
    <tr>
    <% String s = (String) itr.next();%>
    <td> <%=s%></td>
    <td> <%=itr.next()%></td>
    <td> <%=itr.next()%></td>
    <td> <%=itr.next()%></td>
    <td> <%=itr.next()%></td>
    <td> <input type="submit" value="Edit" name="Edit" onclick="editRecord(<%=s%>);"</td>
    <td> <input type="submit" value="Delete" name="Delete" onclick="deleteRecord(<%=s%>);"</td>
                            
     </tr>
<% }%>
</table>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kite
  • 1
  • 1
  • Do you get any errors in console after you submit? – Rishabh Dec 01 '16 at 09:46
  • I don't have any Errors. I tried diff. Codes but its always show empty. – Kite Dec 01 '16 at 09:58
  • I do not know much, but isn't it neccessary for inputs to have forms? – geisterfurz007 Dec 01 '16 at 10:14
  • input tags can be independant. see: http://stackoverflow.com/a/3294624/1107653 and http://www.w3schools.com/TAgs/att_input_form.asp – code_angel Dec 01 '16 at 17:52
  • @Joenel, check if you submit to the right url. Form-action and urlPattern from Servlet must match. Try dummy output from the servlet. Try direct access to servlet though it urlPattern. – code_angel Dec 01 '16 at 18:01
  • Some friends of me has help me out to solve this. My main problem now is how to Iterate Session. btw Thanks for the answer. – Kite Dec 01 '16 at 20:32

0 Answers0