简体   繁体   中英

How do I send data between two jsp?

I want to send the identificator of a product to the shoppingcard page and then display the product on the shoppingcart page I used both request.set attribute and session.setattribute and it doesn't work. If I use session.getattribute or request.getattribute.toString() the page is white nothing is displayed. If I only use request.getAttribute (without toString) the line "ok" is not displayed on the result which means that purchased is null.

ProductStore is a map containing the products we have. Same for shoppingcardStore. ProductBean is the class of the products

products page:

     <h2><a href="<%= "product-page.jsp?id=" + ptp.getId() %>"><%=ptp.getName()%></a></h2>
      <div class="product-btns">
         <form method="GET" action="<%="WhishList.jsp"%>">
            <button class="main-btn icon-btn" name="id" value="<%=ptp.getId()%>"><i class="fa fa-heart"></i></button>
         </form>    
            <button class="main-btn icon-btn"><i class="fa fa-exchange"></i></button>
        <form action="shoppingcard.jsp" method="get">
          <p> <%= ptp.getId() %> </p>
          <%Object product=ptp;
                   request.setAttribute("purchase", ptp.getId());
          %>
          <input type="submit" value="add to cart">
             <button class="primary-btn add-to-cart"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
        </form>
    </div>

shoppingcard page

                ProductStore products = new ProductStore();
                Map<String,ProductBean> prodList = products.getProducts();
                ShoppingcardStore db = new ShoppingcardStore();
                Map<String,ProductBean> list = db.getShoppingcard();
                Object purchased = request.getAttribute("purchase").toString();
                if(purchased!=null){
                    out.println("<h1>Ok</h1>");
                    //ProductBean x = (ProductBean) purchased;
                    String x=(String) purchased;
                    db.Purchase(x);
                    //TODO confirm product has been added to the shoppingcart.
                }%>
                <!-- Product Slick -->
                <div class="col-md-9 col-sm-6 col-xs-6">
                    <div class="row">
                        <div id="product-slick-1" class="product-slick">
                <%  if(list != null){
                    Object[] Shoppingcardlist = list.values().toArray();
                    ProductBean ptp;
                    for(int i = 0; i<Shoppingcardlist.length; i++){
                        ptp = (ProductBean)Shoppingcardlist[i];
                        // TODO display the info of the current wish list.
                %>

of course it is just a part of my code, if you need to see something more tell me.

When you are doing setAttribute(), its scope is limited to the request when the main page is loading and hence will not be available on the next page as it will be a new request.

<%Object product=ptp;
                   request.setAttribute("purchase", ptp.getId());
          %>

What you can do is, submit this value in URL param as GET or in a form (get/ post) to fetch it on next JSP using request.getParameter().

Or you can use session scope by session.setAttribute()

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM