manojvashisht
asked on
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.
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.
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
Andrew
ASKER
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
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
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
ASKER
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
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.
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);
}
ASKER
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.getContextPat h()%>" + "/NotificationManager"");
please tell is there anything wrong with this syntax
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.getContextPat
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);
}
ASKER
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
but now problem is my servlet is not getting called that the image is not changing.
please help with how this could be done
ASKER
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
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".
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".
ASKER
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(requ est.respon seText) > 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
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(requ
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
ASKER
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);
}
if(request.responseText == "<valid>true</valid>")
{
alert("inside toggle if");
toggleIcon(false);
}
else {
alert("inside toggle else");
toggleIcon(true);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hope that helps,
Andrew
Open in new window