我尝试让用户输入特定的商品代码,然后将他们带到一个页面,在那里他们可以根据输入的代码看到代码和数据库中的商品信息。在这第二页上,我希望用户能够输入另一个项目代码(下表)与另一个提交按钮,这将更新与他们添加的另一个项目,从数据库中获得它的表。如何在不丢失表中以前的数据的情况下做到这一点?
到目前为止的代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Scanned Items</title>
</head>
<body>
<h1 align="center"><u><font color="blue">List of Scanned Items</font></u></h1>
<table align="center">
<tr>
<th> Item Code </th>
<th> Name </th>
<th> Price </th>
<th> Taxable </th>
<th> Tax </th>
</tr>
<tr>
<td> ${productcatalogue.code}</td>
<td> ${productcatalogue.name}</td>
<td> $${productcatalogue.price}</td>
<td> ${productcatalogue.taxable}</td>
<td> </td>
</tr>
</table>
</body>
</html>Servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBConnection dbConn = (DBConnection)request.getServletContext()
.getAttribute("dbConn");
ProductCatalogueDAO productcatalogueDAO = new ProductCatalogueDAO();
String itemcode = request.getParameter("itemcode");
ProductCatalogue productcatalogue = productcatalogueDAO.getItems(dbConn.getConnection(), itemcode);
request.setAttribute("productcatalogue", productcatalogue);
RequestDispatcher view = request.getRequestDispatcher
(response.encodeURL("scannedItems.jsp"));
view.forward(request, response);
}发布于 2015-12-02 06:11:43
我可以给你一个概述来处理它,我只是没有使用数据库来存储它,而是其余的事情我都做了。
只要遵循这个顺序,你就会得到你的解决方案,
只需创建一个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);
}-
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;
}
}-输出-

https://stackoverflow.com/questions/34026358
复制相似问题