How do I send data between two jsp?

自作多情 提交于 2019-12-11 15:09:00

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/58606075/how-do-i-send-data-between-two-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!