访问servlet中的javascript对象

时间:2016-01-19 09:18:30

标签: javascript java jquery servlets

以下示例 HTTP POST 请求将发送到 servlet

//create javascript object
var customer = [];    
customer.push({
   "customerID": "123",
   "customerName": "Name One",
   "city": "City One",
});
customer.push({
   "customerID": "124",
   "customerName": "Name Two",
   "city": "City Two",
});
//request to servlet
$.post('MyServletName',{ "customer":customer }).done(function(data) { alert(data); });

问题是如何访问servlet中的customer对象?

我试过以下方式:

Way 1:
    request.getParameterValues("customer") //returns null

Way 2:

    Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        String key = (String) parameterNames.nextElement();
        String val = request.getParameter(key);
        System.out.println(key+": "+val);
    }

    output:
        customer[1][city]: City Two
        customer[1][customerID]: 124
        customer[0][city]: City One
        customer[0][customerID]: 123
        customer[1][customerName]: Name Two
        customer[0][customerName]: Name One

0 个答案:

没有答案