[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Notification for db change on jsp

Hi
I want to show a notification on top of jsp page after a fixed interval of time without refresh.lets say a "new records"  icon if the table has got new records or else the same " no new records" icon .i want to do it with ajax or any best approach in java. then when the user clicks the icon i have to open a modal window kind of thing that hides the parent window,showing all the records for that particular user. any code and approach tried by experts would be really helpful.
0
manojvashisht
Asked:
manojvashisht
  • 7
  • 6
1 Solution
 
gam3r_3xtr3m3Commented:
here's a snipplet that hopefully does just that.

Hope that helps,

Andrew
<script>
 
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
        try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
        try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
        throw new Error( "This browser does not support XMLHttpRequest." )
};
 
window.onload = function() {
 
	// updates every 2.5 seconds
	var msBeforeUpdate = 2500;
	setInterval("checkMe()", msBeforeUpdate);
 
}
 
function checkMe() {
 
        var request =  new XMLHttpRequest();
        request.onreadystatechange = function() {
                if(request.readyState == 4 && request.status == 200) {
 
			// toggles based on returned value
			toggleIcon(parseInt(request.responseText) > 0);
 
                }
        };
        request.open("GET", "checkNewRecords.php");
        request.send(null);
 
}
 
function showRecords() {
 
	if(window.showModalDialog) {
		window.showModalDialog("records.php", "records",
		"dialogWidth: 600px; dialogHeight: 400px");
	} else {
		window.open("records.php", "records",
		"height=600,width=400,toolbar=no,directories=no,status=no,
		menubar=no,scrollbars=no,resizable=no,modal=yes");
	}
 
} 
 
function toggleIcon(stat) {
 
	var recordIcon = document.getElementById("recordIcon");
 
	if(stat) recordIcon.src = "new.png";
	else recordIcon.src = "no_new.png";
 
}
 
</script>
 
<img id="recordIcon" src="no_new.png" onClick="showRecords()" />

Open in new window

0
 
gam3r_3xtr3m3Commented:
I forgot to tell you that you need checkNewRecords.php (or the file you have that checks new records) to output 0 if there are still none and any number greater than 0 if you have new records. oh and apologies for the *.php extensions, just got used to them I forgot yours was *.jsp.

Andrew
0
 
manojvashishtAuthor Commented:
Hi Thanks very much for your response.
but i have a query i am new to ajax so just tell me whether this records.php file name entry is same that i can replace by a java .do class. or would there be a need of a servlet returning a true/false. if you could help me with approach for that or sample code
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gam3r_3xtr3m3Commented:
the "records.php" on the code should be the JSP page for the list of your new records.
the "checkNewRecords.php" on the code should be a servlet returning the number of new records.

the "AJAX" code on this one is only on the checkMe() function, the primary object that handles AJAX is the XMLHttpRequest on JavaScript. note that Java (from Sun) and JavaScript (from Netscape) are not the same bubble, they are different languages for different tasks, only with similar names.

so there, you should actually place the codes above as an output HTML, they aren't really part of the Java language, but a JavaScript / HTML one.


Andrew
0
 
manojvashishtAuthor Commented:
hi
actually i want to know
i have written a servlet which returns:
 <valid>false</valid>   as output a xml.
now i want to use this false object in the ajax for toggling the new records and no_new records images.

i t can be true when there are new records.

please put what i need to change the snippet of code u gave to make it working.
i know i am really demanding but i am a newbie in servlets/ajax.
thanks for giving a helping hand
0
 
gam3r_3xtr3m3Commented:
you should change the part where toggleIcon is called. (see below)

AJAX reads the output of from request.open("GET", "checkNewRecords.php"); and sends it to the request.responseText attribute.


function checkMe() {
 
        var request =  new XMLHttpRequest();
        request.onreadystatechange = function() {
                if(request.readyState == 4 && request.status == 200) {
 
                        // toggles based on returned value
                        //toggleIcon(parseInt(request.responseText) > 0);
                        if(request.responseText == "<valid>true</valid>")
                                toggleIcon(true);
                        else toggleIcon(false);
                }
        };
        request.open("GET", "checkNewRecords.php");
        request.send(null);
 
}

Open in new window

0
 
manojvashishtAuthor Commented:
hi

i have changed the code as u told and put my servlet url in the same.

but now it is giving page error
 expected " ) " on line number ...
which is this line i have checked with view source
  request.open("GET", ""<%=request.getContextPath()%>" + "/NotificationManager"");

please tell is there anything wrong with this syntax
 function checkMe() {
 
        var request =  new XMLHttpRequest();
        request.onreadystatechange = function() {
                if(request.readyState == 4 && request.status == 200) {
 
                        // toggles based on returned value
                        //toggleIcon(parseInt(request.responseText) > 0);
                        if(request.responseText == "<valid>true</valid>")
                                toggleIcon(true);
                        else toggleIcon(false);
                }
        };
        request.open("GET", ""<%=request.getContextPath()%>" + "/NotificationManager"");
        request.send(null);
 
}

Open in new window

0
 
manojvashishtAuthor Commented:
hi i was able to get the error resolved

but now problem is my servlet is not getting called that the image is not changing.

please help with how this could be done
0
 
manojvashishtAuthor Commented:
the window.onload function() is getting called whenever i am reloading ,not even on the first time.

what can be the reason.

thanks in advance
0
 
gam3r_3xtr3m3Commented:
you should change the condition request.responseText == "<valid>true</valid>" to the correct output of the servlet. make sure to check for extra whitespaces and case-sensitivities.

that is actually a fast way to compare, but if you're looking for an XML parser then that should be another thing for JavaScript. note the XMLHttpRequest doesn't really necessarily need a XML reply, a simple plaintext of true/false would do, then change the condition to request.responseText == "true".
0
 
manojvashishtAuthor Commented:
hi
if my servlet is returning a xml how to process that
eg
<data>true<data>

my servlet is not getting a call that is it never enters inside this loop

if(request.readyState == 4 && request.status == 200) {
 
                        // toggles based on returned value
                        //toggleIcon(parseInt(request.responseText) > 0);
                        if(request.responseText == "<valid>true</valid>")
                                toggleIcon(true);
                        else toggleIcon(false);
                }
what may be the problem and how to resolve because nothing is getting updated.
can it be a problem with my servlet?

thanks
0
 
manojvashishtAuthor Commented:
hi my request.responsetext is showing "<valid>true</valid>" as i have taken a .xml file as output to a action that gets called every 2.5 seconds.now when i am comparing it in the code below.it is always going to the else block.please tell whether this way of comparing correct or not

if(request.responseText == "<valid>true</valid>")
                        {
                            alert("inside toggle if");
                                toggleIcon(false);
                                }
                        else {
                         alert("inside toggle else");
                        toggleIcon(true);
                        }
0
 
gam3r_3xtr3m3Commented:
it is correct, though there might be something else going through the responseText, try to check what comes out by doing an alert(request.responseText);
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now