I want field on popup from database

Dani AI

Generated

asked how to populate popup fields from the database; asked for the code and suggested hitting the DB from the JSP. A more maintainable pattern is to separate concerns: keep database access in a servlet/DAO layer (or REST endpoint), return JSON for a single record, and load that JSON into a client-side modal via AJAX. That keeps SQL out of view templates, makes testing easier, and reduces XSS/SQL risks.

A minimal client workflow: when the user requests the popup, the page calls an endpoint like /api/employee?id=123, receives JSON, fills DOM fields, and then opens the modal. Example client code:

fetch('/api/employee?id=' + empId)
  .then(r => r.json())
  .then(data => {
    document.getElementById('empName').textContent = data.name;
    document.getElementById('empEmail').textContent = data.email;
    document.getElementById('empPhoto').src = '/api/employee/photo?id=' + data.id;
    // show modal
  })
  .catch(err => console.error(err));

Practical cautions and tips: always use parameterized queries / PreparedStatement on the server to prevent SQL injection (see OWASP), close DB resources or use a connection pool (prefer a DataSource via the container or HikariCP; Tomcat has JNDI examples), and escape/encode any data you inject into HTML to prevent XSS. Serve photos from a dedicated endpoint that streams bytes with an appropriate Content-Type instead of embedding raw DB blobs into JSP. Use browser DevTools to inspect network requests and JSON payloads while debugging.

References: Using Fetch (MDN), SQL Injection Prevention (OWASP), Tomcat JNDI DataSource examples.

Recommended Answers

All 3 Replies

Post the code you have done so far and provide more details with regard to what you need help with.

<%

   String _empId = (String) request.getAttribute("EmployeeId"); 
   String _flagForPage = (String) request.getAttribute("FlagForPage");


   String redStarStr="*";
   if(_flagForPage!=null && _flagForPage.equals("2"))
   {
       redStarStr="";
   }//end by sanjeevani

   String str1="Edit";
    if(_flagForPage == null)
       _flagForPage = "0"; // means Page Opened for Add mode, if _flagForPage = 1 means Page opened for Edit mode.


    String _selEmpId = (String) request.getAttribute("SelEmpId");
    String _employeeName=(String) request.getAttribute("SelEmpName");

    String _CurrentDate = (String)request.getAttribute("CurrentDate");

    String _FlagForMessage = (String)request.getAttribute("FlagForMessage");

    String _cmd3 = (String)request.getParameter("command3");
    if(_cmd3 == null)
       _cmd3 = "0";
    String _checkMsgFlag = (String) request.getAttribute("checkMsgFlag");

    if(_selEmpId == null)
         _selEmpId = "0"; 


%>

<form name="frmAddPopUp" id="frmAddPopUp" action="AControlServlet" method="POST">

<table class="tbg1" width="100%" border="0" cellpadding="0" cellspacing="0">                 
  <tr><td width="17%" class="padding3" valign="top">
        <table width="100%" border="0" cellpadding="0" cellspacing="0">

    <%   if(_flagForPage == "1" || _flagForPage == "2") { %>
           <tr><td height="4px"></td></tr>
           <tr><td>
           <div id="noPhotoDiv" name="noPhotoDiv" style="display:">
                    <table width="100%" class="T_Border" cellpadding="0" cellspacing="0" border="0" height="180px">
                        <tr><td width="100" height="100"><img src="jsp/image1.jsp?empid=<%=_selEmpId%>" width="215" height="150" > </td></tr>
                        <tr><td class="lebel2" height="10px""><center><%=_employeeName%></center></td></tr>    
                    </table>
            </div>
           </td></tr>
        <% } %>
      </table>
    </td>

           <input type="hidden" id="hselEmpId" value="<%=_selEmpId%>">
                             <!-- Begin code to use Photo imgage name -->
                                <select style="display:none" class="inputbox_250W" id="selEmpPhotoName" name="selEmpPhotoName"  onKeyDown="if(event.keyCode==13) event.keyCode=9;" value="0">
                                    <option value="0">- Select - </option>                                        
                                    <c:forEach var="dispPhotoNm" items="${AllEmployeeDetails}">
                                        <option value="${dispPhotoNm.fempId}" >${dispPhotoNm.fempPhotoNm}</option> 
                                    </c:forEach>
                                </select>

        <td width="83%" class="padding4">
        <!-- Begin Right Side Employee Personal Info Part  -->                         
        <table width="100%" class="T_Border" cellpadding="2" cellspacing="0" border="0">         
        <input type="hidden" id="hFlagForPage" value="<%=_flagForPage%>" >

        <!-- hidden to store 1/0 for javascript function setPrevMsg(flag,eleNm,divNm,divStatus,tdNm,errMsgNo ) -->
            <input type="hidden" id="hFlagForPrevMsg" name="hFlagForPrevMsg" value="0" >
        <!-- hidden to store for whihc Element Nm Error Msg Set -->  
            <input type="hidden" id="hFlagForPrevMsgEleNm" name="hFlagForPrevMsgEleNm" value="" >

I see from your code there is a form "frmAddPopUp". and values to various elements populated request.getAttribute("attribute name").

I think dont want to do this way instead you want to make a database call from jsp itself and propluate the values to various fields on jsp.

If this is the requirement then check these links for more information.

http://forums.mysql.com/read.php?39,20964

You can put the java code that is required to make a jdbc connection in a java scriptlet.Then proceed with what ever you want to do with the connection object using jdbc api.

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.