解决XMLHttpRequest请求的缓存问题

    什么是缓存,就不用解释了, 大家在更新CSDN博客时经常遇到的问题,很头疼. 如何解决浏览器的缓存问题,看例子.

一个访问页面计数器的小例子.不多做解释,直接运行,看运行效果.

AJAXNew.html中:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
           var cachexmlhttp;   
           function cache(){
               //创建XMLHttpRequest对象
               if(window.XMLHttpRequest){
                   //IE7、IE8、FireFox、Mozilla、Safari、Opera
                   cachexmlhttp= new XMLHttpRequest();
                   if(cachexmlhttp.overrideMimeType){
                       cachexmlhttp.overrideMimeType("text/xml");
                   }
               }else if(window.ActiveXObject){
                   //IE6,IE5.5,IE5
                   var activexName=["MSXML2.XMLHTTP.60","MSXML2.XMLHTTP.5.0",
                       "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP",
                        "Miscrosoft.XMLHTTP"];
                    for(var i=0;i<activexName.length;i++){
                        try{
                            cachexmlhttp=new ActiveXObject(activexName[i]);
                            break;
                        }catch(e){
                            
                        }                    
                    }                
               } 
               if(cachexmlhttp===undefined || cachexmlhttp===null){
                   alert("当前浏览器不支持创建XMLHttpRequest对象,请更换浏览器");
                   return;
               }
               //2,注册回调方法
               cachexmlhttp.onreadystatechange=function(){
                   if(cachexmlhttp.readyState===4){
                       if(cachexmlhttp.status===200){
                           var message=cachexmlhttp.responseText;
                           var div=document.getElementById("cachemessage");
                           div.innerHTML=message;
                       }
                   }
               };
              
               //3,设置和服务器端交互的相应参数——Get方式
               cachexmlhttp.open("GET","Cache",true);
               
               //4,设置向服务器端发送的数据,启动和服务器端的交互
               cachexmlhttp.send(null);
           }
           
        </script>     
    </head>
    <body>
        <div id="message"></div>
        <br/>
      <input type="button" οnclick="cache();" value="测试缓存问题"/>
      
      <div id="cachemessage"></div>
    </body>
</html>

Cache.java(一个Servlet)中:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author xuemin
 */
@WebServlet(urlPatterns = {"/Cache"})
public class Cache extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           Integer counter = (Integer)request.getSession().getAttribute("Counter");
             if(counter ==null){
                 counter = 0;
             }else{
                counter++;
             }
             request.getSession().setAttribute("Counter",counter);
             out.println("当前计数器的值为:"+counter);
        } finally {            
            out.close();
        }
    }

运行以上程序, 很明显存在缓存问题,如何解决?

....


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 24
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值