session超时

fzb215 2008-11-15 05:34:59
登陆后在session中设置loginForm成功,但在web.xml中设置了session超时,我就想在服务器在超时(超时时用户又不知道)之前能再将loginForm的一个属性又设置到session中,并在Filter中进行判断再做相应的跳转。
1.在web.xml中配置:
<session-config>
<session-timeout>20</session-timeout>
</session-config>
2.登录时在sessin中设值loginForm
3.当超时那一刻,我希望通过Listener来监听到并重新在session中设值为"mlFlag",该值为loginForm的一个属性:
该选择HttpSessionListener,HttpSessionAttributeListener,HttpSessionBindingListener
的哪一个监听器来设置“mlFlag”。
4.还要在SessionFilter的doFilter方法中获得“mlFlag”:
HttpSession session = ((HttpServletRequest) request).getSession();
if (session.getAttribute("loginForm") == null)
{
String ml = (String)session.getAttribute("mlFlag");
if ( "9".equals(ml)){
response.sendRedirect(hreq.getContextPath() + "/timeout.jsp?mlFlag=" + ml);
}else {
response.sendRedirect(hreq.getContextPath() + "/timeout.jsp?mlFlag=-1");
}
}
我用如下测试过:
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
LoginForm loginForm=(LoginForm)session.getAttribute("loginForm");
session.setAttribute("mlFlag", loginForm.getManagerLevel());
}
在SessionFilter中String ml = (String)session.getAttribute("mlFlag")
这时"ml"为null
先感谢各位朋友了!
...全文
2219 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
fzb215 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 Landor2004 的回复:]
引用 10 楼 fzb215 的回复:
引用 9 楼 fzb215 的回复:
managerLevel总为null原来是过期后Listener中的session和Filter中的session,它们的id不一样了
如果在这种情况下还是要实现session过期后在Filter中做多分支跳转,
考虑过在application中设值,但是不行啊
有什么好方案可以实现?
先谢谢啦

当session过期后,你第一次访问服务器,调用request.getSession()方法的时候,会返回一个新的session,新的id
所以你的思…
[/Quote]
开始没有理解session过期后在HttpSessionListener和Filter中的session的不同,在这种情况下用你的方法还是实现不了我要的功能,
request.getSession(false)来获取session,这样如果session过期了,就返回null
if(request.getSession(false)==null){
//操作
}
这种不能实现由标志值来进行多分支跳转了,
现在的问题就是怎么设置标志值和怎么获得以做判断。
fzb215 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 sunhaochengood 的回复:]
引用 16 楼 fzb215 的回复:
我测试了
在web.xml中配置:
<session-config>
<session-timeout>20 </session-timeout>
</session-config>
session超时时执行了HttpSessionListener的public void sessionDestroyed(HttpSessionEvent se)方法,
没有执行HttpSessionBindingListener的public void valueUnbound(HttpSessionBindingEvent se)方法。
用  sessionDestroyed方法结合Filter就可以实现你的功能。就是说在sessionDe…
[/Quote]
我是结合了Filter,但session过期后Listener中sessionDestroyed方法得到的session和之后Filter中的session,它们不是同一个session了
那在sessionDestroyed方法中对session设的标志值,当过期之后这个session中的标志值找不回来了,再在Filter中通过session.getAttribute()时,得不到标志值就不能在Filter中做多分支跳转了
再用cookie试一试啦。
serryzhao 2008-11-18
  • 打赏
  • 举报
回复
APOLLO_TS 2008-11-18
  • 打赏
  • 举报
回复
占个位置以后看!
sunhaochengood 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 fzb215 的回复:]
我测试了
在web.xml中配置:
<session-config>
<session-timeout>20 </session-timeout>
</session-config>
session超时时执行了HttpSessionListener的public void sessionDestroyed(HttpSessionEvent se)方法,
没有执行HttpSessionBindingListener的public void valueUnbound(HttpSessionBindingEvent se)方法。
[/Quote]用 sessionDestroyed方法结合Filter就可以实现你的功能。就是说在sessionDestroyed对session进行赋值操作!
fzb215 2008-11-18
  • 打赏
  • 举报
回复
我测试了
在web.xml中配置:
<session-config>
<session-timeout>20</session-timeout>
</session-config>
session超时时执行了HttpSessionListener的public void sessionDestroyed(HttpSessionEvent se)方法,
没有执行HttpSessionBindingListener的public void valueUnbound(HttpSessionBindingEvent se)方法。


lyric1812 2008-11-18
  • 打赏
  • 举报
回复
关注学习
lihan6415151528 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Landor2004 的回复:]
session过期了,filter中获取的session也不存在了,session中的东西也都没了,你还能获取得到吗


HttpSession相关监听接口
1.HttpSessionBindingListener接口
注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener

当我们的类实现了HttpSessionBindingListener接口后,只要对象加入Session范围(即调用HttpSession对象的 setAttribute方法的时候)或从Session范围中移出(即调用HttpSession…
[/Quote]

解答的很好!
Landor2004 2008-11-18
  • 打赏
  • 举报
回复
用session肯定是办不到的了
不适用四个作用于吗:page,request,session,application,application是最大的,只要服务器不断,他就不会过期
那你就用application来保存变量!
getdate 2008-11-18
  • 打赏
  • 举报
回复
很好,很有利于学习、、
ngx20080110 2008-11-18
  • 打赏
  • 举报
回复
有學習到技術了
Landor2004 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fzb215 的回复:]
引用 9 楼 fzb215 的回复:
managerLevel总为null原来是过期后Listener中的session和Filter中的session,它们的id不一样了
如果在这种情况下还是要实现session过期后在Filter中做多分支跳转,
考虑过在application中设值,但是不行啊
有什么好方案可以实现?
先谢谢啦
[/Quote]
当session过期后,你第一次访问服务器,调用request.getSession()方法的时候,会返回一个新的session,新的id
所以你的思路是不正确的,过期的session里面的东西都不存在了
你应该建立一个filter,或者在页面上用request.getSession(false)来获取session,这样如果session过期了,就返回null
if(request.getSession(false)==null){
//你的操作
}
fzb215 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 zhzhzhhh 的回复:]
把这个过滤器放置到你多分支跳转的过滤器之前,如11楼所说,
if(request.getSession(false)==null){
//为请求创建一个新的session,然后为session设置属性
}
这样操作应该没有问题吧?
[/Quote]
1.把这个过滤器放置到你多分支跳转的过滤器之前,这个怎么实现,请教!
2.这段代码放在哪里(Filter):
if(request.getSession(false)==null){
//为请求创建一个新的session,然后为session设置属性
}
3.为请求创建一个新的session,然后为session设置属性:
设置属性的值要来自用户登录时的session中的"loginForm"的属性
这个属性在session过期时要保存下来,才能再获得...
或者通其它方式保存?
zhzhzhhh 2008-11-18
  • 打赏
  • 举报
回复
把这个过滤器放置到你多分支跳转的过滤器之前,如11楼所说,
if(request.getSession(false)==null){
//为请求创建一个新的session,然后为session设置属性
}
这样操作应该没有问题吧?
fzb215 2008-11-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fzb215 的回复:]
过期时能在HttpSessionListener的sessionDestroyed方法中可以得到mangerLevel
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
LoginForm loginForm=(LoginForm)session.getAttribute("loginForm");
System.out.println(loginForm.getManagerLevel());  //打印出9
session.setAttribute("mlFlag", loginForm.getManagerLevel());
}
当点击包含".do"的请求时怎么才能到Filt…
[/Quote]
managerLevel总为null原来是过期后Listener中的session和Filter中的session,它们的id不一样了
如果在这种情况下还是要实现session过期后在Filter中做多分支跳转,
考虑过在application中设值,但是不行啊
有什么好方案可以实现?
先谢谢啦!
fzb215 2008-11-17
  • 打赏
  • 举报
回复
过期时能在HttpSessionListener的sessionDestroyed方法中可以得到mangerLevel
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
LoginForm loginForm=(LoginForm)session.getAttribute("loginForm");
System.out.println(loginForm.getManagerLevel()); //打印出9
session.setAttribute("mlFlag", loginForm.getManagerLevel());
}
当点击包含".do"的请求时怎么才能到Filter中的request.gerSession中获得“mlFlag”
if (url.indexOf(".do") >= 0 )
{

HttpSession session = ((HttpServletRequest) request).getSession(true);
if (session.getAttribute("loginForm") == null)
{
String managerLevel = (String)session.getAttribute("mlFlag");
System.out.println(managerLevel + "------------------"); //总为null,怎样在这里获得sessionDestroyed方法中设置的“mlFlag”的值?
if ( "9".equals(managerLevel)){
hrep.sendRedirect(hreq.getContextPath() + "/timeout.jsp?managerLevelFlag=" + managerLevel);
}else {
hrep.sendRedirect(hreq.getContextPath() + "/timeout.jsp?managerLevelFlag=-1");
}
}
else
{
chain.doFilter(request, response);
}
}
可能本人对Filter,Listener相结合操作没理解好,要实现这种功能有什么好方法?
谢谢啦!
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fzb215 的回复:]
谢谢
在这个方法中吗?我试试吧
void valueUnbound(HttpSessionBindingEvent event)
“在处理的后使用跳转就可以了”是什么意思?
项目中是在Filter中跳转的
[/Quote]
你试试,关注ing!
fzb215 2008-11-16
  • 打赏
  • 举报
回复
谢谢
在这个方法中吗?我试试吧
void valueUnbound(HttpSessionBindingEvent event)
“在处理的后使用跳转就可以了”是什么意思?
项目中是在Filter中跳转的
一将天下 2008-11-16
  • 打赏
  • 举报
回复
设置SESSION一个属性,我个人建议用HttpSessionListener
  • 打赏
  • 举报
回复
servlet实现HttpSessionBindingListener接口
在处理的后使用跳转就可以了
加载更多回复(4)

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧