ajax访问servlet时,返回值不同步!
我要实现的效果是:提交后,我要显示post内的进度,简单实现的话就是取time得值。
现在的情况是:可以调到time的值,不过它老是一个值
页面
<script type="text/javascript">
var xh;
function aa(){
setInterval("as()",1000);
}
function as(){
if(document.XMLDocument){
xh = new XMLDocument();
}else{
xh = new ActiveXObject("Microsoft.XMLHTTP");
}
xh.open('get','Ceshi');
xh.onreadystatechange = back;
xh.send(null);
}
function back(){
var ready = xh.readyState;
if(ready==4){
if(xh.status==200){
var str = xh.responseText;
document.getElementById("di").innerHTML=str;
}
}
}
</script>
<form name="form" method="post" action="Ceshi"
enctype="multipart/form-data">
<table>
<tr>
<td>文件路径:</td>
<td><input type="file" ></td>
<td></td>
</tr>
<tr>
<td><input id="submitButton" type="submit" value="上传" onclick="aa();"></td>
</tr>
</table>
</form>
servlet
public class Ceshi extends HttpServlet {
private static String time;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("get....................");
out.println(time+"r");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
time="222222222";
out.println(time+"dddddd");
}
}