【发布时间】:2020-06-29 14:00:30
【问题描述】:
我正在练习 Java ee,并在 jsp 中创建了两个输入字段和按钮。单击“添加”按钮时,我想将这两个字段添加到列表中,然后将它们显示在我的 jsp 页面上。现在,每次单击按钮时它只显示一个结果。我做错了什么?
这是我的jsp:
<body>
<form action="addtocart" method="GET">
Title: <input type="text" name="title"> <br>
Price: <input type="text" name="price"> <br>
<button type="subtmit">Add</button>
</form>
<c:forEach var="bookList" items="${book}">
<ul>
<li>${bookList.name}</li>
<li>${bookList.price}</li>
</ul>
</c:forEach>
</body>
这是我的 servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String title = request.getParameter("title");
double price = Double.parseDouble(request.getParameter("price"));
ArrayList<Book> list = new ArrayList<>();
list.add(new Book(title,price));
session.setAttribute("book", list);
RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
【问题讨论】:
-
我做到了,谢谢你的回答!