0

我目前是 java ee 的新手。我需要有关如何将我的数据从 jsp 传递到我的 servlet 以填写我的查询的帮助。每当我运行时,都会捕获错误并打印出异常。它说java.null.exception ..我找不到我的错误在哪里..请帮助我...谢谢..

这是我的代码:

更新ProductServlet.java

    try {
        String description = request.getParameter("description");
        float price = Float.parseFloat(request.getParameter("price"));
        String size = request.getParameter("size");
        int aisle_id = Integer.parseInt(request.getParameter("aisle"));
        int id = Integer.parseInt(request.getParameter("id"));

        ProductsDao productsDao = new ProductsDao();
        productsDao.updateProduct(description, price, size, aisle_id, id);

    } catch (Exception e) {
        out.println(e);
    }

和我的 ProductDao.java

public void updateProduct(String description, float price, String size, int aisle_id, int id) 
        throws ClassNotFoundException, SQLException {
    Connection conn = null;
    PreparedStatement stmt = null;
    String sql = "UPDATE products "
            + "SET description = ?, price = ?, size = ?, aisle_id = ? "
            + "WHERE id = ? ";

    try {
        conn = ConnectionFactory.getConnection();
        stmt = conn.prepareStatement(sql);
        stmt.setString(1, description);
        stmt.setFloat(2, price);
        stmt.setString(3, size);
        stmt.setInt(4, aisle_id);
        stmt.setInt(5, id);
        stmt.executeUpdate();

    } finally {
        if (conn != null) {
            conn.close();
        }

        if (stmt != null){
            stmt.close();
        }
    }
}

这是我的 ProductById.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <script>
        function functionEdit()
        {
            document.getElementById("edit").type="hidden";
            document.getElementById("update").type="button";
            document.getElementById("delete").type="hidden";
            document.getElementById("cancel").type="button";

            document.getElementById("description").disabled="";
            document.getElementById("size").disabled="";
            document.getElementById("price").disabled="";
            document.getElementById("select").disabled="";
        }

        function functionUpdateRecord(){
            if(confirm('Update existing record?')){
                document.location.href="/MachineProblemWebApp/UpdateProductServlet";
                alert('Update success!');
                document.location.href="/MachineProblemWebApp/GetAllProductsServlet";

            }else{ 
                alert('No changes have been made!');
            }

        }

    </script>
</head>
<body>
    <table border="0">
        <tr>
            <td>id: </td>
            <td><input type="text" name="id" id="id" value="${product.id}" disabled="disabled"></td>
        </tr>
        <tr>
            <td>Description: </td>
            <td><input type="text" name="description" id="description" value="${product.description}" disabled="disabled"></td>
        </tr>
        <tr>
            <td>Aisle </td>
            <td><select name="aisle" disabled="disabled" id="select">
                    <c:forEach var="aisle" items="${aisles}" >
                        <option value="${aisle.id}" ${(aisle.id==product.aisle.id)?"selected" : ""}>${aisle.description}</option>
                    </c:forEach>
                </select></td>
        </tr>
        <tr>
            <td>Size: </td>
            <td><input type="text" name="size" id="size" value="${product.size}" disabled="disabled"></td>
        </tr>
        <tr>
            <td>Price: </td>
            <td><input type="text" name="price" id="price" value="${product.price}" disabled="disabled"></td>
        </tr>
        <tr>
            <td><input type="button" name="edit" id="edit" onClick="functionEdit()" value="Edit" >
                <input type="hidden" name="update" id="update" onClick="functionUpdateRecord()"value="Update">
                <input type="button" name="delete" id="delete" value="Delete">
                <input type="hidden" name="cancel" id="cancel" value="Cancel"></td>
        </tr>
    </table>

</body>

这是堆栈跟踪吗?张贴在 Apache Tomcat 日志中?

Dec 10, 2012 7:38:13 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [UpdateProductServlet] in context with path     [/MachineProblemWebApp] threw exception
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
at java.lang.Float.parseFloat(Float.java:452)
at grocerific.ui.UpdateProductServlet.processRequest(UpdateProductServlet.java:45)
at grocerific.ui.UpdateProductServlet.doGet(UpdateProductServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

Dec 10, 2012 9:36:40 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextDestroyed()
Dec 10, 2012 9:36:40 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextDestroyed()
Dec 10, 2012 9:36:46 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Dec 10, 2012 9:36:46 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Dec 10, 2012 9:36:46 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@8a47d9')
4

2 回答 2

1

看来您尝试传递数据: document.location.href="/MachineProblemWebApp/UpdateProductServlet";

这是不可能的,因为这条线只是重定向到“UpdateProductServlet”而不发送数据。

您需要使用表单标签来提交数据。

在下面的链接中你可以看到如何设置表单标签: http ://www.w3schools.com/html/html_forms.asp

于 2012-12-10T16:49:20.693 回答
0

堆栈跟踪告诉您错误在哪里:

at grocerific.ui.UpdateProductServlet.processRequest(UpdateProductServlet.java:45)

我猜是这条线

float price = Float.parseFloat(request.getParameter("price"));

您传递给 parseFloat 的字符串不是浮点数。它可能是 "" 或 "1,0" 而不是 "1.0" 或类似的。

从架构的角度来看,您完全走错了路。JSP 已经过时并且已知有许多许多问题,您正处于一条非常痛苦的道路的开始。

看看使用速度而不是 JSP。最好研究一些网络应用程序框架。有一些处理表单处理得很好。

于 2012-12-10T16:44:58.467 回答