Hi,
In my jsp page , i have four divs, i want to load dynamically one of the div when i open the page, it has to show the results from the database..I have written the entire code but could not able to load one single div when i open the page in the browser. How to load a div or show the results in <div> when i open the page.

Thanks.

Dani AI

Generated

Quick answer for : there are two reliable ways to ensure one div is populated when the page opens — render the div server‑side in the JSP so it arrives with the HTML, or populate it client‑side after load via AJAX/fetch. — if you actually want a modal/popup, the same injection step applies; you inject content then show the modal. — code examples below demonstrate both approaches so you can pick the one that fits your architecture.

Server-side (JSP) — let the servlet do the DB work, put the results on the request, then forward to the JSP so the div already contains data when the browser renders:

/* servlet */
List<Item> items = dao.findItems(...);
request.setAttribute("items", items);
request.getRequestDispatcher("/page.jsp").forward(request, response);
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<div id="results">
  <c:forEach var="item" items="${items}">
    <div>${item.name}</div>
  </c:forEach>
</div>

Client-side (AJAX) — serve an endpoint that returns an HTML fragment or JSON, then inject it on DOM ready. jQuery example:

$(function(){
  $("#results").load("/dataEndpoint"); // endpoint returns HTML fragment
});

Plain fetch example:

document.addEventListener("DOMContentLoaded", function(){
  fetch('/dataEndpoint')
    .then(r => { if (!r.ok) throw new Error(r.statusText); return r.text(); })
    .then(html => document.getElementById('results').innerHTML = html)
    .catch(console.error);
});

Troubleshooting checklist: confirm the element id matches; check browser console/network for 404/500 or JS errors; ensure servlet returns the expected HTML/JSON and sets contentType; verify your DB query runs on the server (not from the client); if using jQuery include it before your script. If the div is a modal, inject data then toggle visibility. Security note: never expose DB credentials or queries to the client — keep all DB work server‑side and return safe HTML or JSON.

Recommended Answers

All 2 Replies

you mean you want to display the <div> like lightbox or popupbox ...etc when your page is open

could you clarify once how it would be>

If you provide some relevant code that maybe helpful in assisting you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.