简体   繁体   English

如何在两个 jsp 之间发送数据?

[英]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.我想将产品的标识符发送到购物卡页面,然后在购物车页面上显示产品,我同时使用了 request.set 属性和 session.setattribute 并且它不起作用。 If I use session.getattribute or request.getattribute.toString() the page is white nothing is displayed.如果我使用 session.getattribute 或 request.getattribute.toString() 页面为白色,则不会显示任何内容。 If I only use request.getAttribute (without toString) the line "ok" is not displayed on the result which means that purchased is null.如果我只使用 request.getAttribute(没有 toString),则结果中不会显示“ok”行,这意味着购买的是 null。

ProductStore is a map containing the products we have. ProductStore 是包含我们拥有的产品的 map。 Same for shoppingcardStore.购物卡商店也是如此。 ProductBean is the class of the products ProductBean 是产品的 class

products page: products页面:

     <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 shoppingcard页面

                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.当您执行 setAttribute() 时,其 scope 仅限于加载主页时的请求,因此在下一页上将不可用,因为它将是一个新请求。

<%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().您可以做的是,在 URL 参数中以 GET 或表单(get/post)的形式提交此值,以使用 request.getParameter() 在下一个 JSP 上获取它。

Or you can use session scope by session.setAttribute()或者您可以使用 session.setAttribute()

Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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