如何在两个jsp之间发送数据? [英] How do I send data between two jsp?

查看:76
本文介绍了如何在两个jsp之间发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将产品的标识发送到购物卡页面,然后在购物车页面上显示该产品 我同时使用了request.set属性和session.setattribute,但它不起作用.如果我使用session.getattribute或request.getattribute.toString(),该页面为白色,则不会显示任何内容.如果我仅使用request.getAttribute(不带toString),则结果中不会显示"ok"行,这表示购买的商品为空.

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是包含我们拥有的产品的地图.与shoppingcardStore相同. ProductBean是产品的类

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

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

                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.

推荐答案

在执行setAttribute()时,其范围仅限于加载主页面时的请求,因此在下一页将不可用将是一个新请求.

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());
          %>

您可以做的是,在URL参数中以GET或表单(获取/发布)的形式提交此值,以使用request.getParameter()在下一个JSP上获取它.

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().

或者您可以通过session.setAttribute()使用会话作用域

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

希望有帮助

这篇关于如何在两个jsp之间发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆