java “错误HTTP状态500 -对于输入字符串:“”,“添加到订单”功能仅对表中的顶行有效,对其下的任何行无效

b4lqfgs4  于 2022-11-20  发布在  Java
关注(0)|答案(2)|浏览(80)

我目前正在处理一个项目,其中一个方面是将产品添加到订单中。目前,产品显示在表中,每行旁边都有一个按钮“添加到订单”。我遇到的问题是“添加到订单”按钮只对顶行有效,并且在我尝试添加到订单时会导致下面的行/产品出错。如果您能提供任何帮助说明为什么会出现这种情况,我们将不胜感激!谢谢!

错误本身:HTTP状态500 -对于输入字符串:“"(单击第一行下面的”添加顺序“按钮时调用错误。)

注意:使用JSP、java servlet和MySQL工作台。

代码:
“客户产品新.jsp”:

<%    Statement stat = null;
    ResultSet rs = null;
    stat = conn.createStatement(); 
    String search = request.getParameter("productSearch");
    String qry; //sets data variable to hold the SQL query

    String q = request.getParameter("cmbCategory");

    if (search != null) {
        qry = "SELECT productid, pname, category, priceperunit, qtyavailable, pdesc FROM product WHERE pname LIKE '%" + search + "%' OR category LIKE '%" + search + "%' OR priceperunit LIKE '%" + search + "%' OR pdesc LIKE '%" + search + "%'";
    } else {
        qry = "SELECT productid, pname, category, priceperunit, qtyavailable, pdesc FROM product";
    }

    rs = stat.executeQuery(qry); //executes SQL query

%>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Description</th>
                        <th>Stock Available</th>
                        <th>Unit Price</th>
                        <th>Quantity Required</th>
                        <th>Add To Order</th>
                    </tr>
                </thead>
                <tbody>

                    <% while (rs.next()) {%>

                    <tr>    
                        <td width="5%"><input type="text" name="productID" value="<%=rs.getInt("productid")%>" class="border-0" size="1" readonly></td>
                        <td width="5%"><input type="text" name="productName" value="<%=rs.getString("pname")%>" class="border-0" size="20" readonly></td>
                        <td width="8%"><%=rs.getString("category")%></td>
                        <td width="25%"><%=rs.getString("pdesc")%></td>

                        <td width="5%"><%=rs.getDouble("qtyavailable")%></td>
                        <td width="5%"><input type="number" name="pricePerUnit" value="<%=rs.getDouble("pricePerUnit")%>" class="border-0" size="1" readonly></td>

                        <td width="5%"><input id="randomno" type="number" name="quantity" size="1"></td>

                        <td width="5%"><a href="" onclick="this.href='${pageContext.request.contextPath}/orderservlet1?productID=<%=rs.getInt("productid")%>&productName=<%=rs.getString("pname")%>&pricePerUnit=<%=rs.getDouble("pricePerUnit")%>&quantity='+document.getElementById('randomno').value">Add to Order</a></td>                           

                    </tr>

                    <% }%>

                </tbody>
            </table>

”OrderServlet1.java“:

@WebServlet("/orderservlet1")
public class OrderServlet1 extends HttpServlet {

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

         HttpSession session = request.getSession();

        int productID = Integer.parseInt(request.getParameter("productID"));
        String productName = request.getParameter("productName");
        double pricePerUnit = Double.parseDouble(request.getParameter("pricePerUnit"));
        int quantity = Integer.parseInt(request.getParameter("quantity"));

        OrderDAO orderBean = null;

        Object objOrderBean = session.getAttribute("order");

        if (objOrderBean != null) {
            orderBean = (OrderDAO) objOrderBean;
        } else {
            orderBean = new OrderDAO();
            session.setAttribute("order", orderBean);
        }

        orderBean.addOrderItem(productID, productName, pricePerUnit, quantity);
        response.sendRedirect("CustomerProductsNew.jsp");

    }
}

”OrderDAO.java“:

public class OrderDAO {
     private ArrayList allOrderItems = new ArrayList();
    private double orderTotal;

    public int getOrderItemCount() {
        return allOrderItems.size();
    }
 public void addOrderItem(int prodID, String prodName, double pricePerU, int qty) {
        double dblTotalCost = 0.0;
        double pricePerUnit = 0.0;
        int iQty = 0;
        OrderItem orderItem = new OrderItem();
        try {
            pricePerUnit = pricePerU;
            iQty = qty;
            if (iQty > 0) {
                dblTotalCost = pricePerUnit * iQty;
                orderItem.setProductID(prodID);
                orderItem.setProductName(prodName);

                orderItem.setPricePerUnit(pricePerU);
                orderItem.setQuantity(iQty);
                orderItem.setTotalCost(dblTotalCost);
                allOrderItems.add(orderItem);
                calculateOrderTotal();
            }
        } catch (NumberFormatException nfe) {
            System.out.println("Error while parsing from String to primitive types: " + nfe.getMessage());
            nfe.printStackTrace();
        }
    }

    public void addOrderItem(OrderItem orderItem) {
    allOrderItems.add(orderItem);
 }

 public OrderItem getOrderItem(int iItemIndex) {
  OrderItem orderItem = null;
  if(allOrderItems.size()>iItemIndex) {
   orderItem = (OrderItem) allOrderItems.get(iItemIndex);
  }
  return orderItem;
 }

 public ArrayList getOrderItems() {
  return allOrderItems;
 }
 public void setOrderItems(ArrayList allOrderItems) {
  this.allOrderItems = allOrderItems;
 }
 public double getOrderTotal() {
  return orderTotal;
 }
 public void setOrderTotal(double dblOrderTotal) {
  this.orderTotal = dblOrderTotal;
 }

 protected void calculateOrderTotal() {
  double dblTotal = 0;
  for(int counter=0;counter<allOrderItems.size();counter++) {
   OrderItem orderItem = (OrderItem) allOrderItems.get(counter);
   dblTotal+=orderItem.getTotalCost();

  }
  setOrderTotal(dblTotal);
 }

}

”OrderItem.java“:

public class OrderItem {
private int productID;
private String productName;
private String category;
private String pdesc;
private double pricePerUnit;
private int quantity;
private double totalCost;

public int getProductID() {
    return productID;
}

public void setProductID(int productID) {
    this.productID = productID;
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getPdesc() {
    return pdesc;
}

public void setPdesc(String pdesc) {
    this.pdesc = pdesc;
}

public double getPricePerUnit() {
    return pricePerUnit;
}

public void setPricePerUnit(double pricePerUnit) {
    this.pricePerUnit = pricePerUnit;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

public double getTotalCost() {
    return totalCost;
}

public void setTotalCost(double totalCost) {
    this.totalCost = totalCost;
}
}
}

编辑:

必须使用链接到图像,因为我不能把他们直接在这里还。**最终HTML的产品页:**x1c 0d1x

出现错误:

laawzig2

laawzig21#

在“CustomerProductsNew.jsp”中将输入类型编号更改为文本
“class=“border-0”size=“1”只读〉

<td width="5%"><input id="randomno" type="number" name="quantity" size="1"></td>

以上输入类型错误,因为您尝试将整数存储在字符串中
“class=“border-0”size=“1”只读〉

<td width="5%"><input id="randomno" type="text" name="quantity" size="1"></td>
ogq8wdun

ogq8wdun2#

您html结构错误这是解决问题第一步

您的模板

<table>
  <tbody>
    <tr>
    <th>ID</td>
    <th>Name</td>
    <tr>

    <% while (rs.next()) {%>
      <tr>    
        <td>some things</td>
        <td>some things</td>
      </tr>
    <% }%>
  </tbody>
</table>

正确的模板

<table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
      </tr>
    </thead> 
    <tbody>
      <% while (rs.next()) {%>
       <tr>    
         <td>some things</td>
       </tr>
    </tbody>
</table>

大写和小写

警告

<td width="5%"><a href="" onclick="this.href='${pageContext.request.contextPath}/orderservlet1?productID=<%=rs.getInt("productid")%>&productName=<%=rs.getString("pname")%>&pricePerUnit=<%=rs.getDouble("pricePerUnit")%>&quantity='+document.getElementById('randomno').value">Add to Order</a></td>

不是产品Id和pN名称?而是产品ID和p名称。

相关问题