Jsp中解决session过期跳转到登陆页面并跳出iframe框架的方法
程序员文章站
2023-11-27 17:00:04
当session过期后可以用过滤器来设置重定向页面复制代码 代码如下:public class actionfilter extends httpservlet imple...
当session过期后可以用过滤器来设置重定向页面
public class actionfilter extends httpservlet implements filter {
private filterconfig filterconfig;
public void init(filterconfig config) {
this.filterconfig = config;
}
public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws servletexception, ioexception {
httpservletrequest req = (httpservletrequest) servletrequest;
servletrequest.setcharacterencoding(“utf-8″);
httpservletresponse res = (httpservletresponse) servletresponse;
string url = req.getrequesturi();
sysuservoimpl user = (sysuservoimpl) req.getsession().getattribute(“sysuser”);
if (null == user) {
if (!common.isempty(url) && (url.endswith(“newestlogin.jsp”) || url.endswith(“userloginaction.jsp”) || url.endswith(“login.jsp”) || url.endswith(“loginaction.do”))) {
filterchain.dofilter(servletrequest, servletresponse);
} else {
req.getrequestdispatcher(“/newestlogin.jsp”).forward(req, res);
}
} else {
filterchain.dofilter(servletrequest, servletresponse);
}
}
但是这样不能不能跳出iframe等框架。
可以用javascript解决
在你想控制跳转的页面,比如login.jsp中的<head>与</head>之间加入以下代码:
<script language=”javascript”>
if (window != top)
top.location.href = location.href;
</script>
js刷新框架的脚本语句
//如何刷新包含该框架的页面用
<script language=javascript>
parent.location.reload();
</script>
//子窗口刷新父窗口
<script language=javascript>
self.opener.location.reload();
</script>
( 或 <a href="javascript:opener.location.reload()">刷新</a> )
//如何刷新另一个框架的页面用
<script language=javascript>
parent.另一frameid.location.reload();
</script>
如果想关闭窗口时刷新或者想开窗时刷新的话,在<body>中调用以下语句即可。
<body onload="opener.location.reload()"> 开窗时刷新
<body onunload="opener.location.reload()"> 关闭时刷新
<script language="javascript">
window.opener.document.location.reload()
</script>
复制代码 代码如下:
public class actionfilter extends httpservlet implements filter {
private filterconfig filterconfig;
public void init(filterconfig config) {
this.filterconfig = config;
}
public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws servletexception, ioexception {
httpservletrequest req = (httpservletrequest) servletrequest;
servletrequest.setcharacterencoding(“utf-8″);
httpservletresponse res = (httpservletresponse) servletresponse;
string url = req.getrequesturi();
sysuservoimpl user = (sysuservoimpl) req.getsession().getattribute(“sysuser”);
if (null == user) {
if (!common.isempty(url) && (url.endswith(“newestlogin.jsp”) || url.endswith(“userloginaction.jsp”) || url.endswith(“login.jsp”) || url.endswith(“loginaction.do”))) {
filterchain.dofilter(servletrequest, servletresponse);
} else {
req.getrequestdispatcher(“/newestlogin.jsp”).forward(req, res);
}
} else {
filterchain.dofilter(servletrequest, servletresponse);
}
}
但是这样不能不能跳出iframe等框架。
可以用javascript解决
在你想控制跳转的页面,比如login.jsp中的<head>与</head>之间加入以下代码:
复制代码 代码如下:
<script language=”javascript”>
if (window != top)
top.location.href = location.href;
</script>
js刷新框架的脚本语句
复制代码 代码如下:
//如何刷新包含该框架的页面用
<script language=javascript>
parent.location.reload();
</script>
//子窗口刷新父窗口
<script language=javascript>
self.opener.location.reload();
</script>
( 或 <a href="javascript:opener.location.reload()">刷新</a> )
//如何刷新另一个框架的页面用
<script language=javascript>
parent.另一frameid.location.reload();
</script>
如果想关闭窗口时刷新或者想开窗时刷新的话,在<body>中调用以下语句即可。
<body onload="opener.location.reload()"> 开窗时刷新
<body onunload="opener.location.reload()"> 关闭时刷新
<script language="javascript">
window.opener.document.location.reload()
</script>