In the process of Web page production, we often encounter the problem of timely refresh data, if the use of the method, will cause the entire screen flashing refresh effect, which will reduce the user's operational satisfaction.
Therefore, we need a way to achieve the automatic flash data-free method to solve the above problems.
instance to solve the problem:
Want to realize the user after entering the system (the entire session of the time limit), if you receive a new message will send a sound prompts.
Realize the idea:
1. Home part:< Body onload= "init ("); " Call Init (user) at >//load time;
2.js part: Using XMLHTTP to implement page local refresh, call check_mail.jsp to retrieve the background database and return the result.
<!--
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
var checkresult=null;
var username =null;
function init (user) {
Username=user;
Window.setinterval (' Checkmail () ', 5000);//every 5 seconds automatically call 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) {//judged as false
Test1.innerhtml= " ";
Checkresult=1;
}
else{//judgment is true.
Test1.innerhtml= "<ccid_file alt= new mail values=" img/tp024 "
alt= New mail Src=img/tp024.gif/>
<embed src= ' music/nudge.wma ' hidden=true autostart=true loop=false> ';
checkresult=0;
}
}
}
-->
3.check_mail.jsp
<%@ 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.divpareStatement("
select * from message where r_name='"+user+"' and status=0");
ResultSet rs=pStat.executeQuery();
if(rs.next()){//有记录
response.getWriter().print(0);
}else{
response.getWriter().print(1);
}
}finally{
if(conn!=null) conn.close();
}
%>
4. Home results show
Insert < span id= "Test1" > </span > into the specified location.