Ajax实现网页无闪自动局部刷新(邮件接收提示)
2015-07-07 23:16
531 查看
1、首页部分:
<body onload="init('<%=ses_userBean.getUsername()%>');"> // load时调用init(user);2、js部分:xmlhttp实现页面局部刷新,调用check_mail.jsp对后台数据库进行检索判断并返回结果:
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var checkresult=null;
var username =null;
function init(user){
username=user;
window.setInterval('Checkmail()',5000);//每隔5秒自动调用Checkmail()
}
function Checkmail()
{
xmlhttp.open("POST", "check_mail.jsp?uName="+username, false);
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send();
}
function updatePage() {
if (xmlhttp.readyState < 4) {
test1.innerHTML="loading...";
}
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
if(response==1){//判断为假
test1.innerHTML=" ";
checkresult=1;
}
else{//判断为真
test1.innerHTML="<img alt='新邮件' src='img/tp024.gif'><EMBED src='music/nudge.wma' hidden=true autostart=true loop=false>";
checkresult=0;
}
}
}3、check_mail.jsp用于处理科幻段发送上来的请求,并返回对数据库查询得到的结果,有加过返回0,无结果返回1.
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="error/login_error.jsp"%>
<%@ page import="myweb.*" %>
<%@ page import="java.sql.*" %>
<%
String user=request.getParameter("uName");
Connection conn=null;
try{
conn=DBConnection.getConnection();
PreparedStatement pStat=conn.prepareStatement("select * from message
where r_name= '"+user+"' and status=0");
//查询SQL语句
ResultSet rs=pStat.executeQuery();
if(rs.next()){//有记录
response.getWriter().print(0);
}else{
response.getWriter().print(1);
}
}finally{
if(conn!=null) conn.close();
}
%>4、首页结果显示:将<span id="test1"></span>插入指定位置。5、数据库部分,数据库连接部分:略得到效果如图5-6所示。 图5-6 图片自动刷新1、首页部分:
<body onload="init('<%=ses_userBean.getUsername()%>');"> // load时调用init(user);2、js部分:xmlhttp实现页面局部刷新,调用check_mail.jsp对后台数据库进行检索判断并返回结果:
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var checkresult=null;
var username =null;
function init(user){
username=user;
window.setInterval('Checkmail()',5000);//每隔5秒自动调用Checkmail()
}
function Checkmail()
{
xmlhttp.open("POST", "check_mail.jsp?uName="+username, false);
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send();
}
function updatePage() {
if (xmlhttp.readyState < 4) {
test1.innerHTML="loading...";
}
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
if(response==1){//判断为假
test1.innerHTML=" ";
checkresult=1;
}
else{//判断为真
test1.innerHTML="<img alt='新邮件' src='img/tp024.gif'><EMBED src='music/nudge.wma' hidden=true autostart=true loop=false>";
checkresult=0;
}
}
}3、check_mail.jsp用于处理科幻段发送上来的请求,并返回对数据库查询得到的结果,有加过返回0,无结果返回1.
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="error/login_error.jsp"%>
<%@ page import="myweb.*" %>
<%@ page import="java.sql.*" %>
<%
String user=request.getParameter("uName");
Connection conn=null;
try{
conn=DBConnection.getConnection();
PreparedStatement pStat=conn.prepareStatement("select * from message
where r_name= '"+user+"' and status=0");
//查询SQL语句
ResultSet rs=pStat.executeQuery();
if(rs.next()){//有记录
response.getWriter().print(0);
}else{
response.getWriter().print(1);
}
}finally{
if(conn!=null) conn.close();
}
%>4、首页结果显示:将<span id="test1"></span>插入指定位置。5、数据库部分,数据库连接部分:略得到效果如图5-6所示。 图5-6 图片自动刷新1、首页部分:
<body onload="init('<%=ses_userBean.getUsername()%>');"> // load时调用init(user);2、js部分:xmlhttp实现页面局部刷新,调用check_mail.jsp对后台数据库进行检索判断并返回结果:
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var checkresult=null;
var username =null;
function init(user){
username=user;
window.setInterval('Checkmail()',5000);//每隔5秒自动调用Checkmail()
}
function Checkmail()
{
xmlhttp.open("POST", "check_mail.jsp?uName="+username, false);
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send();
}
function updatePage() {
if (xmlhttp.readyState < 4) {
test1.innerHTML="loading...";
}
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
if(response==1){//判断为假
test1.innerHTML=" ";
checkresult=1;
}
else{//判断为真
test1.innerHTML="<img alt='新邮件' src='img/tp024.gif'><EMBED src='music/nudge.wma' hidden=true autostart=true loop=false>";
checkresult=0;
}
}
}3、check_mail.jsp用于处理科幻段发送上来的请求,并返回对数据库查询得到的结果,有加过返回0,无结果返回1.
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="error/login_error.jsp"%>
<%@ page import="myweb.*" %>
<%@ page import="java.sql.*" %>
<%
String user=request.getParameter("uName");
Connection conn=null;
try{
conn=DBConnection.getConnection();
PreparedStatement pStat=conn.prepareStatement("select * from message
where r_name= '"+user+"' and status=0");
//查询SQL语句
ResultSet rs=pStat.executeQuery();
if(rs.next()){//有记录
response.getWriter().print(0);
}else{
response.getWriter().print(1);
}
}finally{
if(conn!=null) conn.close();
}
%>4、首页结果显示:将<span id="test1"></span>插入指定位置。5、数据库部分,数据库连接部分:略得到效果如图5-6所示。 图5-6 图片自动刷新
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- 氪周刊:关注互联网创业
- 据说程序员才会看懂的趣图……
- 数据中心和云未来的十二大趋势
- 《互联网运营智慧》十一月进展
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- 互联网业内的迷雾 谨慎选择虚拟主机
- 互联网应用广泛 电子商务正当时
- 风雨历练十四年 互联网引领信息化
- 观察:中小网站将渐渐成为互联网的主角
- 再谈Jquery Ajax方法传递到action(补充)
- js可突破windows弹退效果代码
- 互联网迅猛崛起 IDC能否迎来发展高峰期
- Dom在ajax技术中的作用说明
- JSP脚本漏洞面面观
- 使用Ajax实时检测"用户名、邮箱等"是否已经存在