没有访问数据库如何获得价值?

时间:2014-12-14 20:23:56

标签: java jsp servlets persist

我需要你的帮助。我目前正在做一个购买项目。用户单击“添加到购物车”后,该项目将添加到购物车,但尚未保留到数据库中。然后,我将让用户点击结帐。 PurchaseCart.jsp将执行操作并将它们带到PurchaseCheckOut servlet。那么,我怎样才能获得之前的数据呢?

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(我已经完成了持久性部分和重定向部分,但我不知道如何获得该值。?是我传入的值。)

 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);
        } 

addPurchasedetails的PurchaseService

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

购买的结构如下:PurchaseM.jsp(让用户选择) - &gt; PurchaseCreate.java(将所有值传递给购物车) - &gt; PurchaseCart.jsp(显示值(尚未保留)) - &gt; PurchaseCheckOut.java(persist) - &gt; AddConfirm.jsp(显示&#34;你已经完成&#34;)

1 个答案:

答案 0 :(得分:2)

您可以将添加到购物车的值存储在Session对象中,并在需要的任何地方进行参考。