Displaying real time data in browser

i m new to ajax i dont know how to display real time data in browser actually i m using applet swing for GUI which will contain JLabels i need to display real time data in those JLabels.
from java class i m able to get those real time data i m storing those values in string array. by using this how can i fetch values in real time manner and display it in browser, i used jsp for displaying applet in browser. in applet windows the data are displaying but when i used jsp for displaying in browser blank JLabels or label text which were default text will come . plz help me by providing sample code snippet

Here is something I wrote within the last few months. It should give an example of something that will work from IE6 and later, as well as FireFox and Chrome.

THIS IS GETTING SERVER TIME, NOT A CLIENT TIMESTAMP.

<span id="svrTime" style="display:block; font-size:16px; font-weight:bold; padding-bottom:5px; text-align:right;"></span>
<span id="svrDay" style="display:block; font-size:16px; font-weight:bold; padding-bottom:5px; text-align:right;"></span>
<span id="svrDate" style="display:block; font-size:16px; font-weight:bold; text-align:right;"></span>

    <script type="text/javascript">
        var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP") ;
        var days = [], months = [];
        days[0] = "Sunday", days[1] = "Monday", days[2] = "Tuesday", days[3] = "Wednesday", days[4] = "Thursday", days[5] = "Friday", days[6] = "Saturday";
        months[0] = "January", months[1] = "February", months[2] = "March", months[3] = "April", months[4] = "May", months[5] = "June", months[6] = "July", months[7] = "August", months[8] = "September", months[9] = "October", months[10] = "November", months[11] = "December";

        function getSvrTD(){
            xhr.open('HEAD',window.location.href.toString(),false);
            xhr.setRequestHeader('Content-Type','text/html');
            xhr.send(null);
            var st = xhr.getResponseHeader("Date"), dt = new Date(st), hour, minute, second, day, month, dayt, year, tz;
            hour = dt.getHours(), minute = dt.getMinutes(), second = dt.getSeconds(), day = dt.getDay(), month = dt.getMonth(), dayt = dt.getDate(), year = dt.getFullYear();
            hour = hour<10 ? "0" + hour : hour ;
            minute = minute<10 ? "0" + minute : minute ;
            second = second<10 ? "0" + second : second ;
            day = days[day];
            month = months[month];
            dayt = dayt<10 ? "0" + dayt : dayt ;
            tz = tz == 360 ? " (CDT)" : " (CST)" ; // Only works for CENTRAL time zone; modify as needed.
            document.getElementById("svrTime").innerHTML = hour + ":" + minute + ":" + second;
            document.getElementById("svrDay").innerHTML = day;
            document.getElementById("svrDate").innerHTML = month + " " + dayt + ", " + year + tz;
            }
        setInterval("getSvrTD()",1000);
    </script>

HTH,

:slight_smile:

thanks for your reply
but i m not getting how to fetch from a java class
say String array val,which will get data continuously from data server i need to fetch it as it comes to display it in browser

This isn’t really a web question, this is a Java Swing question. Applets are not web pages and work much differently (and probably should be avoided at all costs).

If you want to do it as a website, you would probably encode your array as a JSONArray, then print that out and ask for it with a JavaScript heartbeat and update the UI when necessary.

But you’re using Java Swing and I know pretty much nothing about Swing or Java GUI apps in general. Just because applets run in a browser, doesn’t mean they are actually related to the browser or web technology. It’s actually the opposite, it’s a plugin that runs in a completely separate process with a completely separate set of rules.

then how to make it real time ?

Sorry if you misunderstood. I don’t know and you’re probably not going to find help here (though someone might come along). You’re asking on the wrong forum. This is a very Java question and this is a Web Developer forum. The only web tech you’re using here, is the JSP and that has really nothing to do with the application you’re working on.

Sorry… I saw the OP mention “AJAX”, so immediately switched to JS-mode. :smile: I just assumed (which given the frequency, is understandable) that the OP confused Java with JavaScript.

So… uh… hey, there’s a free JS AJaX script for getting server time/date, if anyone wants it. :grimacing:

:slight_smile:

2 Likes

I think he’s confusing a Java Applet with Web tech (HTML/CSS/JS).

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.