“错误HTTP状态500-对于输入字符串:“”,添加到订单功能仅适用于表中的第一行,不适用于其下面的任何行

问题描述 投票:0回答:1

我目前正在从事一个项目,其中一个方面是向订单中添加产品。当前,产品显示在表格中,每行旁边都有一个“添加到订单”按钮。我遇到的问题是“添加到订单”按钮仅适用于第一行,当我尝试添加到订单时,将导致下面的一行/产品出现错误。对于这种情况的任何帮助,将不胜感激!谢谢。

错误本身:HTTP状态500-对于输入字符串:“”(当我单击第一行下方的添加订单按钮时​​,调用错误。)

注意:使用JSP,Java Servlet和MySQL工作台。

代码:

“ CustomerProductsNew.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">
                <tbody>
                    <tr>
                        <th>ID</td>
                        <th>Name</td>
                        <th>Category</td>
                        <th>Description</th>
                        <th>Stock Available</th>
                        <th>Unit Price</th>
                        <th>Quantity Required</th>
                        <th>Add To Order</th>

                    </tr>


                    <% 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;
}
}
}
java mysql jsp servlets dao
1个回答
0
投票

您的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>                           

不是乘积I d和p N ame吗?而是productid和pname。

© www.soinside.com 2019 - 2024. All rights reserved.