检查是否单击了按钮jsp

时间:2018-09-24 01:24:51

标签: java html jsp servlets html-form

我正在尝试使用Java逻辑来检查是否从外部JSP单击了HTML表单按钮。该项目是一个Web“购物车”程序,我的目标是对JSP逻辑进行编码,以便可以将从cart.jsp中添加,存储和显示从index.html添加的项目。我在检查是否在cart.jsp的index.html中单击了按钮时遇到了麻烦。如何将这种逻辑写入我的jsp文件?

cart.jsp,index.html:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="style.css" />
        <title>cart</title>
    </head>
    <%
        int i = 0;
        List itemList = new ArrayList();
        List priceList = new ArrayList();
        Double subtotal = 0.0;
        if (subtotal == 0) {

        }
    %>

    <%
        int counter = 0;
        int amt1 = 0;
        int amt2 = 0;
        int amt3 = 0;

        String item1 = request.getParameter("item1");
        String item2 = request.getParameter("item2");
        String item3 = request.getParameter("item3");

        if (item1 != null) {
            priceList.add(0.50);
            itemList.add("item1");
            subtotal = cart("Pencil", 0.50);
            amt1 += 1;
        }

        if (item2 != null) {
            priceList.add(0.50);
            itemList.add("item1");
            amt2 += 1;
        }

        if (item3 != null) {
            priceList.add(0.50);
            itemList.add("item1");
            amt3 += 1;
        }
    %>
    <%!
        public Double cart(String itemName, Double price) {
            Double sub = 0.0;
            return sub;
        }

    %>
    <body>
        <h1>cart</h1>
        <table class="table">
            <%
                if (itemList.size() <= 0) {
                    out.println("Your shopping cart is empty.");
                } else {
            %>
            <thead>
                <tr>
                    <th scope="col">description</th>
                    <th scope="col">amount</th>
                    <th scope="col">price</th>
                </tr>
            </thead>
            <tbody>
                <%
                    for (int x = 0; x < priceList.size(); x++) {
                %>
                <tr>
                    <td>
                        <%
                            out.println(priceList.get(x));
                        %>
                    </td>
                    <td>
                        <%
                            out.println(priceList.get(x));
                        %>
                    </td>
                </tr>
                <%
                    }
                %>
                <tr>
                    <td>
                        grand total:
                    </td>
                    <td>
                        grand total:
                    </td>
                </tr>
            </tbody>
            <%
                }
            %>
        </table>
        <br/><br/>
        <a href="index.html">
            <button type="button" name="back" class="submit" style="width: 15%; text-align: center; margin: auto;">back</button>
        </a>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <title>form</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <!-- Form section start -->
        <div class="section">
            <div class="tablelayout">
                <form action="cart.jsp" onsubmit="cart.jsp">
                    <table class="table">
                        <thead>
                            <tr>
                                <th scope="col">item</th>
                                <th scope="col">description</th>
                                <th scope="col">price</th>
                                <th scope="col">add to cart</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td></td>
                                <td>item1</td>
                                <td>$7.25</td>
                                <td><button type="button" name="item1" class="submit">add</button></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td>item2</td>
                                <td>$3.50</td>
                                <td><button type="button" name="item2" class="submit">add</button></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td>sticker</td>
                                <td>$0.75</td>
                                <td><button type="button" name="item3" class="submit">add</button></td>
                            </tr>
                        </tbody>
                    </table>
                    <button onclick="cart.jsp" type="button" class="submit" style="text-align: center; padding: 1%; margin: 20px; width: 15%;">view cart</button>
                </form>
            </div>
        </div>
        <!-- Form section end -->
    </body>
</html>

enter code here

1 个答案:

答案 0 :(得分:0)

首先,您必须将index.html更改为index.jsp 然后您可以使用为您提供true ya false值的按钮请求参数