在GET servlet中将数组作为参数传递

时间:2017-07-31 07:48:34

标签: ajax servlets parameter-passing

我正在尝试将数组传递给servlet中的GET请求,但它一直出现500错误。我哪里做错了?我console.log'所有的数组(类型,长度等),他们都很好。 doGet中的Types.length给出NullPointerExecption。

JS:

function updatePrice(types, lengths, heights, grits, cuts, quantities) {
console.log(types);
$.get("GetPrice", {
    types: types,
    lengths: lengths,
    heights: heights,
    grits: grits,
    cuts: cuts,
    quantities: quantities },
    function(responseText) {
        $("#createOrderCost").empty().append(responseText);
    });
}

doGet(使用@WebServlet(“/ GetPrice”)定位):

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String Types[] = request.getParameterValues("types"); 
    String Lengths[] = request.getParameterValues("lengths"); 
    String Heights[] = request.getParameterValues("heights"); 
    String Grits[] = request.getParameterValues("grits"); 
    String Cuts[] = request.getParameterValues("cuts"); 
    String Quantities[] = request.getParameterValues("quantities");
    double totalPrice = 0;
    int lengthConvert = 0;
    double lengthPrice = 0;
    for (int i = 0; i < Types.length; i++) {
        Product currProduct = ProductDAO.getProduct(Types[i], Integer.parseInt(Heights[i]), Integer.parseInt(Grits[i]), Integer.parseInt(Cuts[i]));
        lengthConvert = Integer.parseInt(Lengths[i])/1000;
        lengthPrice = lengthConvert*currProduct.getPrice();
        totalPrice += lengthPrice*Integer.parseInt(Quantities[i]);
    }
        String text = Double.toString(totalPrice);
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8"); 
        response.getWriter().write(text);       
}

500错误:

jul. 31, 2017 3:54:22 PM org.apache.catalina.core.StandardWrapperValve 
invoke
SEVERE: Servlet.service() for servlet [webservices.GetPrice] in context with 
path [/Nordic_Abrasive_Online] threw exception
java.lang.NullPointerException
at webservices.GetPrice.doGet(GetPrice.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案