We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Notification for db change on jsp

Medium Priority
490 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

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

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

Author

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
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

Author

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
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

Author

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

Author

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

Author

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
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".

Author

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

Author

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);
                        }
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);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.