这是我的 Servlet,其中将文本框值添加到 ArrayList。我也在使用 JavaBeans。
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String companyName = request.getParameter("txtCompany");
double price = Double.parseDouble(request.getParameter("txtPrice"));
HttpSession session = request.getSession();
// Invoice r = new Invoice();
ArrayList<Invoice> list = (ArrayList<Invoice>) session
.getAttribute("EInvoice.list");
if (list == null) {
list = new ArrayList<Invoice>();
}
list.add(new Invoice(companyName, price));
session.setAttribute("EInvoice.list", list);
String url = "/Show.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
rd.forward(request, response);
}
这是 Show.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show</title>
</head>
<body>
<c:forEach var="list" items="${EInvoice.list}">
<h1>${list.companyName} ${list.price}</h1>
</c:forEach>
</body>
</html>
我希望它显示输入的文本框值,但我得到的只是一个空白页。知道为什么吗?在我学习 JSTL 时,请原谅任何愚蠢的代码错误。