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 .
DatabasesjQueryJSPAJAX

Avatar of undefined
Last Comment
puneet kumar

8/22/2022 - Mon
Julian Hansen

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.
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 .
Julian Hansen

Are you saying you want to populate the ENTIRE table using AJAX?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
puneet kumar

ASKER
yes julian absolutely right i want to exactly like this .please send me some code snippet with ajax.
Julian Hansen

What does your JSP return - can you give me an example?
puneet kumar

ASKER
jsp return a vector julian.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Julian Hansen

jsp return a vector julian.
This does not mean anything.

I need to see the actual data that is returned.
puneet kumar

ASKER
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
puneet kumar

ASKER
Thanx a ton in support.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
puneet kumar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question