让你的页面永不超时
当你使用一个系统时,如果长时间不操作就会出现超时现象,如果你正在编辑一个文档,这时你修改的内容可能就白费了哦。怎么办?让你的会话不超时呗。
解决超时问题有如下方法:
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>
页:
[1]