需要帮助将数据从jsp传递到servlet

时间:2012-12-10 16:05:32

标签: java jsp servlets

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

这是我的代码:

UpdateProductServlet.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 Log?

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')

2 个答案:

答案 0 :(得分:1)

您似乎尝试传递数据白: document.location.href="/MachineProblemWebApp/UpdateProductServlet";

并且这是不可能的,因为这一行只是重定向到&#34; UpdateProductServlet&#34;没有发送数据。

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

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

答案 1 :(得分:0)

stacktrace告诉你错误的位置:

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

我猜这一行

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

传递给parseFloat的字符串不是float。它可能是“”或“1,0”而不是“1.0”或类似。

从架构的角度来看,你完全走错了路。 JSP已经过时,并且在一条非常痛苦的道路上就已经知道有许多问题。

使用velocity而不是JSP。更好地研究一些Web应用程序框架。有一些处理形式处理非常好。