<html>
<head>
<title>View Shopping Cart</title>
<script type="text/javascript">
function remove(book_id)
{
document.getElementById('bookId').value = book_id;
document.forms[0].submit();
}
</script>
</head>
<body>
<table border="1" id="bookTable">
<tr>
<th>Book ID</th>
<th>Book Name</th>
<th>Author</th>
<th>Quantity</th>
<th>Remove from cart</th>
</tr>
<%
try
{
int cus_id = Integer.parseInt(session.getAttribute("cusId").toString());
ResultSet rs = SqlConnection.getData("select * from book b, shopping_cart s where s.book_id=b.book_id AND s.cus_id=" + cus_id);
if (!rs.first())
{
out.print("<h3>Nothing in the Cart!</h3>");
}
else
{
rs.previous();
while (rs.next())
{
%>
<tr>
<%
out.print("<td>" + rs.getInt("book_id") + "</td>");
out.print("<td>" + rs.getString("book_name") + "</td>");
out.print("<td>" + rs.getString("author_name") + "</td>");
out.print("<td><input type='number' name='qty' id='qty'/></td>");
out.print("<td><input type='button' onClick='remove(" + rs.getInt("book_id") + ")' value='Remove from cart'/></td>");
%>
</tr>
<%
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
%>
</table><br/><br/>
<p style="color: green;">${successful}</p> <br/>
<p style="color: red;">${error}</p> <br/>
<a href="viewBooks.jsp"><input type="button" value="View Books"></a>
<a href="welcome.jsp"><input type="button" value="Home"></a>
<form action="/ShoppingCartRemoving" method="post">
<input type="hidden" name="bookId" id="bookId"/>
</form>
</body>
</html>
这是JSP代码。我检查并意识到这从未进入servlet。但另一个具有相同结构的JSP页面工作正常。这令人困惑。我试图从购物车中删除商品。
JavaScript函数是获取bookId并将其放在表单中隐藏的输入字段中,然后提交表单。这个确切的代码在另一个JSP页面中运行良好。
答案 0 :(得分:0)
我发现了问题。这是浏览器。我改变浏览器后,它工作正常。代码很好而且正确。