Search code examples
javajspservletspersist

How to get value without accessing database?


I need your help. I'm currently doing a project on purchase. After the user click on Add to cart, the item will be added to the cart but not yet persist into database. Then later on, I will have the user to click checkout. PurchaseCart.jsp will do action and bring them to PurchaseCheckOut servlet. So, how can I get the data from previous ?

PurchaseCart.jsp

<body>
          <%!List<Double> stockArray = new ArrayList<Double>() ;%>

     <%!List<Object> list1 = new ArrayList<Object>();%>

     <% Object o = request.getAttribute("purchased"); 
     list1.add(o); 
     int size = list1.size(); 
     double stockPrice = (Double)request.getAttribute("stockPriceReal"); 
      if(stockArray.size() == 0)
      { 
         stockArray.add(stockArray.size(),stockPrice);
      } 
      else {

stockArray.add(stockArray.size(), stockPrice);  }  %>
<form action = "../PurchaseCheckOut"> 
   <table border="1">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Purchase Details ID</th>
                    <th>Stock ID</th>
                    <th>Quantity</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>

                    <% for (int g = 0; g < size; g ++) { %>
                <tr>
                   <% String toString = list1.get(g).toString(); 
                    String subString1 = toString.substring(0,7);
                    String subString2 = toString.substring(7,9);
                    String subString3 = toString.substring(9,14);
                   %>
                    <td><%= g +1 %></td>
                    <td><%= subString1 %></td>
                    <td><%= subString2 %></td>
                    <td><%= subString3 %></td>
                    <td><%= stockArray.get(g).toString() %></td>


                </tr>

                <% } %>
            </tbody>
        </table>
            <input type = submit name="checkout" value="Check Out">
            </form>
     </body>

PurchaseCheckOut.java (I've done the persist part and redirect part but I have no idea how to get the value. ? is the value i'm passing in.)

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            PurchaseService ps = new PurchaseService(em);
            utx.begin();
            boolean success = ps.addPurchaedetails(?);
            utx.commit();
            HttpSession session = request.getSession();
            session.setAttribute("success", success);
            response.sendRedirect("MemberAccess/AddConfirm.jsp");
        }
        catch (Exception ex) {
            Logger.getLogger(PurchaseCheckOut.class.getName()).log(Level.SEVERE, null, ex);
        } 

PurchaseService for addPurchasedetails

 public boolean addPurchasedetails(Purchasedetails purcD) {
    mgr.persist(purcD);
    return true;    }

Structure of the purchase works like this : PurchaseM.jsp(let user to choose) -> PurchaseCreate.java(pass all the value to cart) -> PurchaseCart.jsp (display the value(not yet persist))-> PurchaseCheckOut.java(persist) -> AddConfirm.jsp(display "You've done")


Solution

  • You can store the add to cart value in a Session object and refer anywhere you need to.