继续属性到下一个请求的页面

时间:2018-07-12 18:02:50

标签: javascript java mysql servlets

最初,将发布一个充满值的表,并使用javascript编码提供带有下拉按钮的列。当我单击“放置”按钮时,会弹出一个对话框,显示是或否,如果单击“是”,我希望将该特定订单转移到另一个servlet(/放置)。这是包含所有订单的订单页面(/ order)。如何将订单从此页面(/ order)移至放置页面(/ drop),并从/ order页面删除该特定订单? 这就是我所做的,但是我遇到了错误。

(/ order)

    HttpSession session = request.getSession();
        if (session.getAttribute("user_name") != null) {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Payroll", "root", " ");
            String userName = (String) session.getAttribute("user_name");
            PreparedStatement ps = con.prepareStatement("SELECT * from Payroll.Order order by delDate asc");

            ResultSet rs = ps.executeQuery();


            out.println("<script type=\"text/javascript\">" 
                    + "function getConfirmation(){"
                    + "var retVal = confirm(\"Add to Drop List?\");"
                    + "if( retVal == true ){"
                    + "document.location=\"DvDrop\";"
                    + "return true;"
                    + "}"
                    + "else{"
                    + "document.location=\"DvOrders\";"
                    + "return false;"
                    + "}"
                    + "}"
                    + "</script>" );

            out.println(<strong>Welcome " + session.getAttribute("user_name") + "</strong>");

            out.println("<br><h2><center><b>Order List</b></center></h2><br><table border=\"1\" style=\"width:100%;border-spacing: 0.5em;text-align: center;\">");
            out.println("<tr><td><b>Order ID</b></td><td><b>Ordered Date</b></td><td><b>Ordered Time</b></td><td><b>Total Amount</b></td><td><b>Delivery Date</b></td><td><b>Add to Drop List</b></td></tr></b>");
            while (rs.next()) {
                out.println("<tr><td><a href = # style=\"width:90%;\">"+ rs.getString(1) + "</a></td><td>" + rs.getString(2) + "</td><td>" + rs.getTime(3) + "</td><td>" + rs.getDouble(4) + "</td><td>" + rs.getDate(5) + "</td>");
                out.println("<td>" + "<input type=\"button\" value=\"Add to Drop\" name=\"AddDrop\" onClick=\"getConfirmation()\">" + "</td></tr>");
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } else {
            response.sendRedirect("login.jsp?id=Your session may be expired. You have to login first");

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

(/ drop)

PrintWriter out = response.getWriter();
    try {
        // PrintWriter out = response.getWriter() ;
        HttpSession session = request.getSession();
        if (session.getAttribute("user_name") != null) {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Payroll", "root", " ");
            String userName = (String) session.getAttribute("user_name");
            String order = (String)session.getAttribute("Orderid");
            PreparedStatement ps = con.prepareStatement("delete * from Payroll.Order where Orderid = '" + order + "'");

            ResultSet rs = ps.executeQuery();

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

1 个答案:

答案 0 :(得分:0)

我假设您在代码中遇到Java编译错误。如果您用于/order servlet的代码与您在此处发布的代码相同,则在您的代码的

out.println(<strong>Welcome " + session.getAttribute("user_name") + "</strong>");

有一个错误。您尚未正确定义用于将HTML发送到浏览器的字符串。您需要在double quotes标记之前使用<strong>,这似乎会导致Illegal start of expression错误

尝试一下。

HttpSession session = request.getSession();
    if (session.getAttribute("user_name") != null) {

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Payroll", "root", " ");
        String userName = (String) session.getAttribute("user_name");
        PreparedStatement ps = con.prepareStatement("SELECT * from Payroll.Order order by delDate asc");

        ResultSet rs = ps.executeQuery();


        out.println("<script type=\"text/javascript\">" 
                + "function getConfirmation(id){"
                + "var retVal = confirm(\"Add to Drop List?\");"
                + "if( retVal == true ){"
                + "document.location=\"DvDrop?Orderid=" + id + "\";"
                + "return true;"
                + "}"
                + "else{"
                + "document.location=\"DvOrders\";"
                + "return false;"
                + "}"
                + "}"
                + "</script>" );

        out.println("<strong>Welcome " + session.getAttribute("user_name") + "</strong>");

        out.println("<br><h2><center><b>Order List</b></center></h2><br><table border=\"1\" style=\"width:100%;border-spacing: 0.5em;text-align: center;\">");
        out.println("<tr><td><b>Order ID</b></td><td><b>Ordered Date</b></td><td><b>Ordered Time</b></td><td><b>Total Amount</b></td><td><b>Delivery Date</b></td><td><b>Add to Drop List</b></td></tr></b>");
        while (rs.next()) {
            out.println("<tr><td><a href = # style=\"width:90%;\">"+ rs.getString(1) + "</a></td><td>" + rs.getString(2) + "</td><td>" + rs.getTime(3) + "</td><td>" + rs.getDouble(4) + "</td><td>" + rs.getDate(5) + "</td>");
            out.println("<td>" + "<input type=\"button\" value=\"Add to Drop\" name=\"AddDrop\" onClick=\"getConfirmation(\'" + rs.getString(1) + "\')\" />" + "</td></tr>");
        }
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
    } else {
        response.sendRedirect("login.jsp?id=Your session may be expired. You have to login first");

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

在这里,由于您需要将特定项目添加到放置列表中,因此建议您以这种方式创建按钮:单击该按钮时,它将发送要放置的订单的ID。建议的更改已在上面的代码中进行。我正在执行的操作是将当前订单的ID发送为函数getConfirmation()的参数。如果需要,您可以查看使用浏览器的检查工具生成的最终HTML内容。您会看到每个按钮调用相同的功能,但具有相应的订单ID

此外,在您的/drop Servlet中,您试图执行数据操作(插入/更新/删除),但是您正在使用executeQuery()方法。而是尝试使用executeUpdate(),类似这样的

PrintWriter out = response.getWriter();
try {
    // PrintWriter out = response.getWriter() ;
    HttpSession session = request.getSession();
    if (session.getAttribute("user_name") != null) {

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Payroll", "root", " ");
        String userName = (String) session.getAttribute("user_name");
        //String order = (String)session.getAttribute("Orderid");
        String order = Integer.parseInt(request.getParameter("Orderid"));
        PreparedStatement ps = con.prepareStatement("delete * from Payroll.Order where Orderid = '" + order + "'");

        int rs = ps.executeUpdate();

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

除此之外,在您的JavaScript代码中,您正在为窗口分配一个新位置,然后紧接着,您尝试返回一些内容。在为文档分配新位置后,当前脚本便在那里结束,新网页的脚本开始执行。

我还建议对您的JavaScript代码进行一些更改。他们在第一个代码段中。希望这会有所帮助!

干杯!