Java 中的 servlet

发布于 2024-10-27 16:22:16 字数 675 浏览 1 评论 0原文

我想要的是显示一个产品列表,每个产品都有一个“添加到购物车”按钮。我如何将该按钮与产品关联起来?

即,如果我单击将项目 A 添加到购物车,我将如何在按钮中指定调用 ShoppingCart 类,从哪里添加该项目。

我已经有一个 servlet,用于将所有产品填充到 ArrayList 中,并将其转发到 JSP 页面。

<form action="products" method="get">
<table border=1>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.id}</td>
            <td>${product.description}</td>
            <td>${product.unitPrice}</td>
            <td><input type="button" value="Add to cart" onlick="????"/></td>
        </tr>
    </c:forEach>
</table>
</form>

What I want is to display a list of products with an "add to cart" button for each product. How will I associate that button with reference to the product?

i.e If I click to add an Item A to the cart, how will I specify in the button, to call the ShoppingCart class, from where the item will be added.

I already have a servlet, for populating all the products into an ArrayList, and the forward it to a JSP page.

<form action="products" method="get">
<table border=1>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.id}</td>
            <td>${product.description}</td>
            <td>${product.unitPrice}</td>
            <td><input type="button" value="Add to cart" onlick="????"/></td>
        </tr>
    </c:forEach>
</table>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叹梦 2024-11-03 16:22:16

将表单放在 中,而不是放在桌子周围。使用 让按钮真正发挥作用,而不需要 JavaScript 破解,而 JavaScript 破解在禁用 JS 的网络浏览器上无论如何都会失败。在表单中包含 以传递产品 ID。

总结:

<table border=1>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.id}</td>
            <td>${product.description}</td>
            <td>${product.unitPrice}</td>
            <td>
                <form action="products" method="get">
                    <input type="hidden" name="id" value="${product.id}" />
                    <input type="submit" value="Add to cart"/>
                </form>
            </td>
        </tr>
    </c:forEach>
</table>

这样,它将在监听 /products 的 servlet 的 doGet() 方法中可用,如下所示。

String id = request.getParameter("id");
// ...

与具体问题无关,您希望使用 POST 而不是 GET 来实现此目的。否则,当用户为结果页面添加书签并稍后打开它时,或者当她/他只是单击/跟随另一个人传递的链接(潜在的恶意)电子邮件/即时消息/网站/等中的人。将 method="get" 替换为 method="post" 并将 doGet() 中的逻辑移至 doPost() >。

Put the form in the <td> instead of around the table. Use <inupt type="submit"> to let the button really function without the need for JavaScript hacks which would fail anyway on webbrowsers with JS disabled. Include a <input type="hidden"> in the form to pass the product ID.

Summarized:

<table border=1>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.id}</td>
            <td>${product.description}</td>
            <td>${product.unitPrice}</td>
            <td>
                <form action="products" method="get">
                    <input type="hidden" name="id" value="${product.id}" />
                    <input type="submit" value="Add to cart"/>
                </form>
            </td>
        </tr>
    </c:forEach>
</table>

This way it'll be available as follows in doGet() method of the servlet listening on /products.

String id = request.getParameter("id");
// ...

Unrelated to the concrete problem, you'd like to use POST for this instead of GET. Otherwise the user will be able to add another product to his basket when s/he bookmarks the result page and opens it later, or when s/he just clicks/follows a link passed by another (potential malicious) person in an email/IM/site/etc. Replace method="get" by method="post" and move logic in doGet() to doPost().

撩动你心 2024-11-03 16:22:16
 <td><a href="/yourShopKeeperServlet?productId=${product.id}"> <~-- Your Image Goes here --> </a></td>

它将 GET 到 servlet。从那里你可以阅读

request.getParameter("productId");
 <td><a href="/yourShopKeeperServlet?productId=${product.id}"> <~-- Your Image Goes here --> </a></td>

It will make GET to servlet. and from there you can read

request.getParameter("productId");
仙女山的月亮 2024-11-03 16:22:16

我已经从事 jsp 工作很多年了,但我可以给您概念并将实现留给您。

  • 在每个 tr 中,您将有一个表单,其中 item_id 的隐藏值和表单中的按钮作为提交操作,并且另一个名为“add”的隐藏项
  • 每个表单将提交到您的一个 servlet,让我们命名它
  • 在servlet中的shoppingcart中,在处理请求时,您将看到是否有一个名为“add”的参数。如果是,那么您将获得名为“item_id”的参数并将该商品添加到购物车。

been ages tat i have worked on jsp's but i can give you the concept and leave the implementation to you.

  • in each tr you will have a form, with hidden value of item_id and button in the form as submit action, and another hiden item named as "add"
  • each form will submit to your one servlet, lets name it shoppingcart
  • in servlet, while processing request, you will see if there is a parameter named "add". if yes, then you will get the parameter named "item_id" and will add that item to cart.
海夕 2024-11-03 16:22:16

您可以使用产品 id 调用一个函数:

<td><input type="button" value="Add to cart" onlick="addToCart("+${product.id}+");"/></td>

然后 ajax 调用

function addToCart (id) {
    $.post("/cart", {prodId: id}, callback};
}

servlet 将类似于

public class CartServlet extends HttpServlet {
    public void doPost (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String prodId= req.getParameter("prodId");
        // continue with the purchasing code
    }
}

You would call a function using the product id:

<td><input type="button" value="Add to cart" onlick="addToCart("+${product.id}+");"/></td>

and then an ajax call

function addToCart (id) {
    $.post("/cart", {prodId: id}, callback};
}

the servlet would be something like

public class CartServlet extends HttpServlet {
    public void doPost (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String prodId= req.getParameter("prodId");
        // continue with the purchasing code
    }
}
傲世九天 2024-11-03 16:22:16

我对这些答案的怀疑是,在 TR 或 TD 内的每种表单中,都会出现整页发布请求,这对最终用户来说将是性能问题。每次单击“添加到购物车”按钮时,他都必须等待页面加载。您可以做的是调用 ajax 函数并使用 ITEM_ID 发布 URL,这样就不会导致您发布整页内容。要启用购物车条目,请使用内联 JSP,该 JSP 将使用表单发布进行刷新,以便获取购物车条目的详细信息。

或者,您可以调用 JSP 函数将项目添加到数组中,提交请求时将向该数组传递 Session 对象。有很多帖子可以让您更好地了解此实现。

使用 scriptlet 调用 onClick 函数

My doubt around these answers is that in each form within TR or TD there will be full page post request will occur that will be performance issue for the end user. He has to wait for page load every time he clicked the Add to cart button. What you can do is call an ajax function and post the URL with the ITEM_ID so that it will not cause you full page post. To enable the cart entry have a inline JSP that will be refreshed with the Form Post in order to get the details of the Cart entries.

Alternatively You can call a JSP function to add the Items in the Array that Will be passed a Session object while submitting the request. There is lot of posts are present to Give you better understanding of this implementation.

onClick function call using scriptlet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文