jsp中禁止页面缓存的方法

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    response.setHeader("Pragma","No-cache");  //HTTP 1.1     
    response.setHeader("Cache-Control","no-cache");//HTTP 1.0       
    response.setDateHeader("Expires",0); //防止被proxy
    //System.out.println(basePath + "loginAction!checkLog");
    //response.sendRedirect(basePath+"loginAction!checkLog");
%>

以上代码就是jsp文件中的,这句:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

主要是用来得到一个路径,所以在页面中路径都变成了这样:

<link href="<%=path%>/view/utils/style/jquery.alerts.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript" src=\'#\'" /view/utils/js/jquery.alerts.js"></script>


而这三句:

response.setHeader("Pragma","No-cache");  //HTTP 1.1      

response.setHeader("Cache-Control","no-cache");//HTTP 1.0        

response.setDateHeader("Expires",0); //防止被proxy

就是为了禁止页面缓存的,相当于html中的:

<meta http-equiv="Pragma" content="no-cache">    

<meta http-equiv="Cache-Control" content="no-cache">

<meta http-equiv="Expires" content="0">



哈哈,继续努力,我现在已经能看懂很多jsp里的java或者jsp标记了!!!

你可能感兴趣的:(jsp中禁止页面缓存的方法)