0

Following is sample HTTP POST request which is sent to 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); });

Question is how to access the customer object in servlet?

I have tried following way:

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
3
  • 1
    Use any json library to parse it. Commented Jan 19, 2016 at 9:20
  • By the looks of it in "way 2" you are accessing the customer information Commented Jan 19, 2016 at 9:22
  • @PatrickEvans Not the way I want to access it, How can I identify that which value belongs to which row and which variable? Commented Jan 19, 2016 at 9:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.