Link to home
Start Free TrialLog in
Avatar of puneet kumar
puneet kumar

asked on

how to send ajax call to java servlet and retrieve a database table having 5 columns data in json and render it to jsp for display

how can i send ajax call to database and retrieve a table having 5 columns data as json  and render in jsp to display .
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

AJAX render to JSP does not make sense.

AJAX calls a JSP page (optionally with parameters)
The JSP page makes a call to the database to retrieve the data.
The JSP script sends the data back as a JSON string.
The AJAX callback receives the JSON return and populates the display.
[Assuming jQuery]
$.ajax({
  url: 'yourscript.jsp',
  data: {}, // Any data you need to send to your script
  type: 'POST',
  dataType: 'JSON'
}).then(function(resp) {
   // Your script sends back data as an array of objects
   if (resp) {
     resp.forEach(function(item) {
        // item is an object containing the 5 columns of data
        // Populate the page
     });
   }
});

Open in new window


You have given us very little information so it is difficult to give code on how to display the the data on your page - you will need to tell us more about your page and how you want it updated before we can provide the code.
Avatar of puneet kumar
puneet kumar

ASKER

hi jualian let me explain you may be it clears you.let me now if you need more explanation . in my earlier questions you saw that what i want to achieve please refer  my below code -

<tr>
    <th>Check/UnCheck</th>
      <th>MYear</th>
      <th>P Date</th>
      <th>Q Date</th>
      <th>B Date</th>
  </tr>
  <tr>
    <td align="top">
      <input type="checkbox" class="chkValues" id="ckbCheckAll" name="chkBox" type="checkbox" data-row="1" value="" size="50">
    </td>
    <td align="left">
      <input class="inputText row1" type="text" name="mYear" id="mYear"  value="2020">
    </td>
    <td>
      <input class="date row1" id="pDate1" name="dob" type="date" value="2020-01-01" />
    </td>
    <td>
      <input class="date row1" id="qDate1" name="dob" type="date" value="2020-01-01" />
    </td>
    <td>
      <input class="date row1" id="bDate1" name="dob" type="date" value="2020-01-01" />
    </td>
  </tr> 
  <tr>
    <td align="top">
      <input type="checkbox" class="chkValues" id="ckbCheckAll" name="chkBox" type="checkbox" data-row="2" value="" size="50">
    </td>
    <td align="left">
      <input class="inputText row2" type="text" name="mYear" id="mYear"  value="2020">
    </td>
    <td>
      <input class="date row2" id="pDate2" name="dob" type="date" value="2020-02-01" />
    </td>
    <td>
      <input class="date row2" id="qDate2" name="dob" type="date" value="2020-02-01" />
    </td>
    <td>
      <input class="date row2" id="bDate2" name="dob" type="date" value="2020-02-01" />
    </td>
  </tr> 
</table>   

Open in new window




now i am achieving this trough back end like give some url on html in tag action="test.jsp"  its taking to much time that's why i want to  achieve this by ajax call .same situation is that in above html code you see that 4 filed's are there with checkbox the fields are in database are
mYear , pDate2 , qDate2 , bDate2 . all fields are i want to fetch with ajax call and display in my jsp page . hope this clears you the requirement please let me know if you need more explanation .
Are you saying you want to populate the ENTIRE table using AJAX?
yes julian absolutely right i want to exactly like this .please send me some code snippet with ajax.
What does your JSP return - can you give me an example?
jsp return a vector julian.
jsp return a vector julian.
This does not mean anything.

I need to see the actual data that is returned.
Hi Julian i returned a value from data like below


Map ls1 = new LinkedHashMap();
   
                  for (int i = 1; i < 2 ; i++) {
                        ls1.put("1",tableData);
                  }
            
                  
                  JSONArray jsonArrayDetails = new JSONArray();
                  jsonArrayDetails.add(ls1);
                  System.out.println("obj1.toJSONString()-->" + jsonArrayDetails.toJSONString());
                  out.print(jsonArrayDetails.toJSONString());
                  
                  
in browser response i am getting the value like below -

[{"1":["16\/01\/2020","17\/01\/2020","18\/01\/2020"]}]


but in jsp dynamic id is there so how can i show this 3 dates value in jsp in dd/mm/yy formate
Thanx a ton in support.
ASKER CERTIFIED SOLUTION
Avatar of puneet kumar
puneet kumar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial