让你的页面永不超时

当你使用一个系统时,如果长时间不操作就会出现超时现象,如果你正在编辑一个文档,这时你修改的内容可能就白费了哦。

怎么办?让你的会话不超时呗。

解决超时问题有如下方法:

1 将你的服务器的超时时间设置得很长,这显然增加了服务器的负担(让会话永不超时更是不可取的方式)

2 使用iframe定期向服务器发送请求,自动延长超时时间.(当然这个iframe是隐藏的)

3 使用ajax程序代替iframe定期向服务器发送请求,自动延长超时时间。

感觉用ajax的方式实现起来比iframe更加正规灵活。写了个程序,这次还是整合到了dojo中了

先看一下js文件:

//创建一个session激活类

dojo.provide("dojo.noSessionTimeOut.SessionInvoker");
dojo.noSessionTimeOut.SessionInvoker =function(){
 


}

//定义定期激活session方法
dojo.noSessionTimeOut.SessionInvoker.prototype.invoke=function(){
        dojo.io.bind({
       url: window.contextPath+"/servlet/InvokeSession",
       mimetype: "text/json",
       timeoutSeconds: 3000,
       method: "POST",
       content: {},
       load: function(type, data, evt) {
         
       
       },
       error: function(type, error) { alert(error.message); },
       timeout: function(type) { }
     });
    
}

//这是定义服务器端代码,简单的访问session对象,以保持连接
public class InvokeSession extends HttpServlet {
 private static Logger logger = Logger.getLogger(InvokeSession.class);
 
 /**
  * Constructor of the object.
  */
 public InvokeSession() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doPost(request,response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        logger.info("激活session开始");
  request.getSession().getCreationTime();
  logger.info("激活session结束");
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
 public void init() throws ServletException {
  // Put your code here
 }

}


在jsp 页面上定期调用这个对象的触发函数,需要注意的是,触发函数的定期运行时间要小于session的超时时间,我设的是10分钟^_^

<script type="text/javascript">
//这里是将contextPath设置到客户端,以方便获取资源
    window.contextPath="<%=request.getContextPath()%>";
</script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/dojo040/dojo.js"></script>
<script type="text/javascript">
//导入会话激活包
 dojo.require("dojo.noSessionTimeOut.*");
//创建会话激活对象
 var invoker=new dojo.noSessionTimeOut.SessionInvoker();
 //这里是指定在页面加载完毕后需要执行的脚本
     dojo.addOnLoad(function(){
//这里定期进行激活对象激活函数的调用
          window.setInterval(invoker.invoke,10*1000*60);
  
     });
  
 
</script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值