1

我有这样的Java代码,包my.ebill.pro.db;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import newt.com.properties.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


@WebServlet("/BG_Form")
public class BG_Form extends MyDbConn {

    /**
     * 
     */
    private static final long serialVersionUID = -2319984741700546377L;
    static Statement st,st1;

    public BG_Form() throws Exception {
        super();
        st=con.createStatement();
        st1=con.createStatement();
    }

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

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();


    String poNumberBas=request.getParameter("poNo");
    String fromPoBas=request.getParameter("toAddress");
    String toPoBas=request.getParameter("toWhomname");
    String[] productsBas = request.getParameterValues("product");
    String[] pricesBas= request.getParameterValues("price");
    String[] qtysBas= request.getParameterValues("qty");
    String[] linetotalsBas = request.getParameterValues("linetotal");
         System.out.println("Product"+productsBas);
    /*out.println( poNumber + fromPo+toPo+products+prices+qtys+linetotals+"Adress");*/
    PoGet poget=new PoGet();
    poget.setFromPo(fromPoBas);
    poget.setToPo(toPoBas);
    poget.setProducts(productsBas);
    poget.setPrices(pricesBas);
    poget.setPoNumber(poNumberBas);
    poget.setQtys(qtysBas);
    poget.setLinetotals(linetotalsBas);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

这里是 getter 和 setter;

public class PoGet {

    private String poNumber;
    private String fromPo;
    private String toPo;
    private String[] products;
    private String[] prices;
    private String[] qtys;
    private String[] linetotals;
}

而jsp代码是

<html>
<head>
<%@include file="includes/headscript.jsp" %> 
 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>
</head>
<body>
<form id="validationform" name="validationform" method="get" action="BG_Form">
<input type="text" style="border-right-width: 2px; margin-right: 0px; margin-left: 33px; margin-top: -15px;" id="F_Date" class="dateRagePicker leftPick1 hasDatepicker" maxlength="20" size="20">
<table class="order-list" style="margin-left: 228px;">
    <thead>
        <tr><td>Product</td><td>Price</td><td>Qty</td><td>Total</td></tr>
    </thead>

    <tbody>
        <tr>
            <td><input type="text" name="product"></td>
            <td><input type="text" name="price">
            </td><td><input type="text" name="qty"></td>
            <td><input type="text" readonly="readonly" name="linetotal"></td>
            <td><a class="deleteRow"> x </a></td>
        </tr>
    </tbody>

    <tfoot>
        <tr>
            <td style="text-align: center;" colspan="5">
                <input type="button" value="Add Product" name="addrow" id="addrow">

            </td>
        </tr>

        <tr>
            <td colspan="5">
                Grand Total: Rs<span id="grandtotal"></span>
            </td>
        </tr>
        <tr>
        <td>
        In Words <span id="inworDs" ></span>

        </tr>
        <tr>
                <td>
                <input type="submit"/>

        </tr>
    </tfoot>
</table>

</form>



<script>
    $("#addrow").click(function(){
      var newRow = $("<tr>");
              var cols = "";
              cols += '<td><input type="text" name="product"/></td>';
              cols += '<td><input type="text" name="price"/></td>';
              cols += '<td><input type="text" name="qty"/></td>';
              cols += '<td><input type="text" name="linetotal" readonly="readonly"/></td>';
              cols += '<td><a class="deleteRow"> x </a></td>';
              newRow.append(cols);
        $("table.order-list").append(newRow);
    });
    $("table.order-list").on("change", 'input[name^="price"], input[name^="qty"]', function (event) {
        calculateRow($(this).closest("tr"));
        calculateGrandTotal();
    });

    $("table.order-list").on("click", "a.deleteRow", function (event) {
        $(this).closest("tr").remove();
        calculateGrandTotal();
    });


function calculateRow(row) {
    var price = +row.find('input[name^="price"]').val();
    var qty = +row.find('input[name^="qty"]').val();
    row.find('input[name^="linetotal"]').val((price * qty).toFixed(2));
}


function calculateGrandTotal(f) {
    var grandTotal = 0;
    $("table.order-list").find('input[name^="linetotal"]').each(function () {
        grandTotal += +$(this).val();
            });
    $("#grandtotal").text(grandTotal.toFixed(2));

}
</script>
</body>
</html>

从jsp我得到多个值字符串数组,

谁能告诉我如何检索和设置我从 javascript 发送的值。

我需要获取值必须将其传递给数据库。

4

1 回答 1

2
String[] productsBas = request.getParameterValues("product");
String[] pricesBas= request.getParameterValues("price");
 String[] qtysBas= request.getParameterValues("qty"); 

for(int i=0;i<productsBas.length;i++){

 get values here using

  productsBas[i]  & pricesBas[i] and qtysBas[i] then save it
}
于 2013-04-19T13:54:30.433 回答