Sir, I need to retrieve the records from the database for every 7 seconds and display those records in a jsp.Following is my code. x.jsp: <%@page import="com.tbss.RealDAO"%> <%@page import="java.util.List"%> <%@page import="com.tbss.RtChannels"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <script type="text/javascript" language="javascript"> function postRequest(strURL) { //document.writeln("pr"); if (window.XMLHttpRequest) { // Mozilla, Safari, ... var xmlHttp = new XMLHttpRequest(); }else if (window.ActiveXObject) { // IE var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlHttp.open('POST', strURL, true); //xmlHttp.setRequestHeader // ('Content-Type', 'application/x-www-form-urlencoded'); //document.writeln("pr1"); xmlHttp.onreadystatechange=function(){processRequest(xmlHttp);}; xmlHttp.send(null); } function processRequest(xmlHttp) { //document.writeln("prq"); if (xmlHttp.readyState == 4) { if(xmlHttp.status == 200) { //get the XML send by the servlet var profileXML = xmlHttp.responseXML.getElementsByTagName("Profile")[0]; //Update the HTML updateHTML(profileXML); else { alert("Error loading page\n"+xmlHttp.status+":"+xmlHttp.statusText); } } } /** * This function parses the XML and updates the * HTML DOM by creating a new text node is not present * or replacing the existing text node. */ function updateHTML(profileXML) { document.writeln("updateHTML"); //The node valuse will give actual data var profileText = profileXML.childNodes[0].nodeValue; //Create the Text Node with the data received var profileBody = document.createTextNode(profileText); //Get the reference of the DIV in the HTML DOM by passing the ID var profileSection = document.getElementById("display"); //Check if the TextNode already exist if(profileSection.childNodes[0]) { //If yes then replace the existing node with the new one profileSection.replaceChild(profileBody, profileSection.childNodes[0]); } else { //If not then append the new Text node profileSection.appendChild(profileBody); } } function calls(){ //document.writeln("before"); postRequest('/AutoDialer/refreshServlet'); } </script> </head> <body onload="calls()"> <div id="display"> <table border="5"> <tr> <th>COMPAIGN_ID</th> <th>RECORD_ID</th> <th>SESSION_ID</th> <th>CONNECTEDPHONE</th> <th>SESSION_DURATION</th> </tr> <c:forEach var="rc" items="${listKey}"> <tr bgcolor="grey"> <td><c:out value="${rc.compaignID}"></c:out></td> <td><c:out value="${rc.recordID}"></c:out></td> <td><c:out value="${rc.sessionID}"></c:out></td> <td><c:out value="${rc.connectedPhone}"></c:out></td> <td><c:out value="${rc.sessionDuration}"></c:out></td> </tr> </c:forEach> </table> </div> </body> </html> In between the <div></div> I have to display the records. I am displaying the records using jstl tag libraries. But it is not working. I think it is not right way. I have written a following servlet to get the records from the database dynamically. - RefreshServlet.java package com.tbss; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class RefreshServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 6523265408718669299L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet"); HttpSession session=request.getSession(); PrintWriter out=response.getWriter(); // TODO Auto-generated method stub response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); List<RtChannels> list; RealDAO rdao=new RealDAO(); rdao.store(); list=rdao.getRecords(); System.out.println("list: "+list); session.setAttribute("listKey",list); List<RtChannels> list1=(List<RtChannels>) session.getAttribute("listKey"); //out.print(list1); out.print("<Profile><![CDATA[" + list1 + "]]></Profile>"); out.close(); } } Here am getting the records as a list of pojo classes. I have to display these list of records in between <div></div>. Following is my DAO class - RealDAO.java package com.tbss; import java.io.Serializable; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.timesten.jdbc.TimesTenConnection; import com.timesten.jdbc.TimesTenDataSource; public class RealDAO implements Serializable{ /** * */ private static final long serialVersionUID = 7075169571642208856L; long s,e; TimesTenConnection ttcon; TimesTenDataSource ttds; Statement stmt; ResultSet rset; Date start=null,end=null; DateFormat df=new SimpleDateFormat("HH:mm:ss"); public RealDAO() { } public void store(){ start=new Date(); String startString=df.format(start); try { ttds = new TimesTenDataSource(); ttds.setUrl("jdbc:timesten:client:dsn=ttc1;UID=ttsa;PWD=abc123"); ttcon = (TimesTenConnection) ttds.getConnection(); stmt = ttcon.createStatement(); //ttds.setLoginTimeout(30); stmt.executeUpdate("insert into RT_CHANNELS values(1,1,'a1',9581135170)"); stmt.executeUpdate("insert into RT_CHANNELS values(2,2,'a2',9985424475)"); stmt.executeUpdate("insert into RT_CHANNELS values(3,3,'a3',9885922704)"); stmt.executeUpdate("insert into RT_CHANNELS values(4,4,'a4',9849281031)"); stmt.executeUpdate("insert into RT_CHANNELS values(5,5,'a5',9490183642)"); stmt.executeUpdate("insert into RT_CHANNELS values(6,6,'a6',9246543352)"); stmt.executeUpdate("insert into RT_CHANNELS values(7,7,'a7',9595959595)"); stmt.executeUpdate("insert into RT_CHANNELS values(8,8,'a8',5956565656)"); stmt.executeUpdate("insert into RT_CHANNELS values(9,9,'a9',4854232125)"); stmt.executeUpdate("insert into RT_CHANNELS values(10,10,'a10',8956565555)"); s=start.getTime(); ttcon.commit(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { stmt.close(); ttcon.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public List<RtChannels> getRecords(){ List<RtChannels> list=new ArrayList<RtChannels>(); try { ttds = new TimesTenDataSource(); ttds.setUrl("jdbc:timesten:client:dsn=ttc1;UID=ttsa;PWD=abc123"); ttcon = (TimesTenConnection) ttds.getConnection(); stmt = ttcon.createStatement(); ttds.setLoginTimeout(30); // create the TimesTen data source and set the connection URL //ttcon.setTtPrefetchClose(false); rset=stmt.executeQuery("select * from rt_channels"); while(rset.next()) { RtChannels rp=new RtChannels(); rp.setCompaignID(rset.getLong("CAMPAIGN_ID")); rp.setConnectedPhone(rset.getLong("CONNECTEDPHONE")); rp.setRecordID(rset.getLong("RECORD_ID")); rp.setSessionID(rset.getString("SESSION_ID")); end=new Date(); String endString=df.format(end); /* int endInt=Integer.parseInt(endString);*/ e=end.getTime(); rp.setSessionDuration(endString); rp.setSessionDuration("00:00:00"); start=end; list.add(rp); } //System.out.println("return"); } catch(SQLException e) { e.printStackTrace(); } finally{ try { //System.out.println("getrecordfinal"); rset.close(); stmt.close(); ttcon.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } } And ajax code is not fetching the updated data from the database. After getting the response from the servlet am unable to display the records. please help me in resolving this problem. Please send me your answer to Anugandula.Suresh@Tata-bss.com
Use the following:
<META HTTP-EQUIV="Refresh" CONTENT="7">