我可以给你概述来处理它,我只是没有使用数据库来处理它
存储它,但剩下的事情我做了。
只要按照这个顺序,你就会得到你的解决方案,
只需创建一个 jsp 文件,在我的例子中是 a1.jsp
------------------------ a1.jsp 代码---------------- -----------------
<%@page import="java.util.ArrayList"%>
<%@page import="com.stackoverflow.ItemBean"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<table border="2">
<tr>
<th>Item Code</th>
<th>Name</th>
<th>Price</th>
<th>Taxable</th>
<th>Tax</th>
<th>Action</th>
</tr>
<%
ArrayList<ItemBean> list = new ArrayList<ItemBean>();
if(session.getAttribute("allitems") != null){
list = (ArrayList<ItemBean>)session.getAttribute("allitems");
}
for(ItemBean ib : list){
%>
<tr>
<td><%= ib.getItemCode() %></td>
<td><%= ib.getItemname() %></td>
<td><%= ib.getPrice() %></td>
<td><%= ib.getTaxable() %></td>
<td><%= ib.getTax() %></td>
<td><i>Saved</i></td>
</tr>
<%}%>
<form action="ItemSubmit.do" method="post">
<tr>
<td><input type="text" name="itemCode" value="" /></td>
<td><input type="text" name="itemname" value="" /></td>
<td><input type="text" name="price" value="" /></td>
<td><select name="taxable">
<option value="yes">Yes</option>
<option value="no">No</option>
</select></td>
<td><input type="text" name="tax" value="" /></td>
<td><input type="submit" value="Add" /></td>
</tr>
</form>
</table>
</body>
</html>
--------------- ItemSubmit.java servlet 代码 -----
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
ArrayList<ItemBean> listBean = new ArrayList<ItemBean>();
String itemCode = request.getParameter("itemCode");
String itemname = request.getParameter("itemname");
double price = 0;
String taxable = request.getParameter("taxable");
double tax = 0;
String remark = "saved";
try {
price = Double.parseDouble(request.getParameter("price")) ;
tax = Double.parseDouble(request.getParameter("tax"));
} catch (Exception e) {
remark = e.getMessage();
}
if(session.getAttribute("allitems") != null){
listBean = (ArrayList<ItemBean>)session.getAttribute("allitems");
}
ItemBean itemBean = new ItemBean(itemCode, itemname, price, taxable, tax,remark);
//save itemBean to DB-if needed then...
listBean.add(itemBean);
session.setAttribute("allitems",listBean);
request.getRequestDispatcher("/a1.jsp").forward(request, response);
}
-------- ItemBean.java 普通 java 类 ---------------
package com.stackoverflow;
public class ItemBean {
String itemCode;
String itemname;
double price;
String taxable;
double tax;
String remark;
public ItemBean(String itemCode, String itemname, double price, String taxable,double tax , String remark ) {
this.itemCode = itemCode;
this.itemname = itemname;
this.price = price;
this.taxable = taxable;
this.tax = tax;
this.remark = remark;
}
public String getItemCode() {
return itemCode;
}
public void setItemCode(String itemCode) {
this.itemCode = itemCode;
}
public String getItemname() {
return itemname;
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTaxable() {
return taxable;
}
public void setTaxable(String taxable) {
this.taxable = taxable;
}
public double getTax() {
return tax;
}
public void setTax(double tax) {
this.tax = tax;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
--------------- 输出 --------