Я пытаюсь использовать Java-логику, чтобы проверить, нажата ли кнопка формы Html из внешнего JSP.Проект представляет собой веб-программу для создания корзины покупок, и моя цель состоит в том, чтобы закодировать логику JSP, чтобы элементы, добавленные из index.html, можно было добавлять, сохранять и отображать в / из cart.jsp.У меня проблемы с проверкой, нажата ли кнопка в index.html из cart.jsp.Как я могу написать эту логику моего файла 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