这个在网页文件中加上每次刷新都去强制去服务器检查
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);
解决方案 »
- weblogic+myeclipse初学struts2部署时总是Could not load user defined filter in web.xml
- 用jQuery写的javascript在windows下一般用什么调试
- 请问大家有职业适应题吗?
- 如何实现 论坛的系列版块的 展开收缩..
- log4j的问题
- 大过年的又要麻烦大侠了,请问这程序要怎样调才能正常运行啊?(问题解决后送100分)
- 大家帮着看一下这段java代码的问题,关于通过sql语句返回记录集的。
- 菜鸟问题,请各位帮忙!
- 在线bug系统,求高手指导
- mybatis中注解方式调用存储过程返回参数如何获取
- 关于javascript的问题,大家来帮帮我
- 软件自动更新的问题
也是超时的话,转到登陆页面。
你要做个监听,对session来监听
你要在web.xml文件中把他加载进去,让服务器启动的时候就开始监听
代码如下:import java.sql.SQLException;
import java.util.Hashtable;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;public class Listener implements HttpSessionListener,
HttpSessionAttributeListener {
private Hashtable usertable = new Hashtable(); private String username = null; public synchronized void sessionCreated(HttpSessionEvent arg0) {
arg0.getSession().setMaxInactiveInterval(1200);//设置超时时间
} public synchronized void sessionDestroyed(HttpSessionEvent arg0) {
username = (String) usertable.get(arg0.getSession().getId());
usertable.remove(arg0.getSession().getId());
arg0.getSession().removeAttribute("userID");
String loginTime = arg0.getSession().getAttribute("loginTime").toString();
OperateDB operateDB = new OperateDB();
try {
operateDB.executeUpdate("delete from pdm_login_user where cuser_id = '"+username+"' and clogin_time = '"+loginTime+"'");
} catch (SQLException e) {
e.printStackTrace();
}
} public synchronized void attributeAdded(HttpSessionBindingEvent arg0) {
if (arg0.getSession().getAttribute("userID") != null) {
usertable.put(arg0.getSession().getId(), arg0.getSession()
.getAttribute("userID"));
}
} public synchronized void attributeRemoved(HttpSessionBindingEvent arg0) { } public synchronized void attributeReplaced(HttpSessionBindingEvent arg0) { }
}
可是从你的代码中只能看出能自行删除了数据库中的相应记录,并不能看出是如何自动返回登陆页面的呀?谢谢.
<script language="javascript">
var TIMEOUT_SECONDS=30;
function refreshThisPage(){
window.location.reload();
}
function alertTimeout(){
if( window.confirm('快要超时了,你要刷新么?'))
refreshThisPage();
}
//这句不要放到任何 function 里面去了,页面装载到这儿就执行了。
setTimeout('alertTimeout()', (TIMEOUT_SECONDS - 5 )*1000); // 提前5分钟通知。
}
</head>如果要自动刷新,不用用户干涉:
<meta name="refresh" content="30;url=hello.jsp" >
<!-- 30 秒获刷新,如果刷新本页,请去掉 ;url=hello.jsp -->
但这样感觉还是有问题,我如果只是在页面上输入一些东西没有提交,这样页面并没有刷新,但是我是有在页面上做事情的,session也不会计超时,那我不刷新页面,这个提醒对话框到了25分钟不管你有没有在页面上响应都会弹出来,是不是会这样??楼上的兄弟
我们用的是struts,在action中封装了一层,在上层中检验session中的值是否已经删除即是否超时。超时的话就在上层类中转向登陆页面。如果没有超时的话,就执行他的子类,
即用户每操作一次都检查一次
啊.......太好了,我想知道的就是哪个action的父类中相关功能是如何实现的?附:引:"即用户每操作一次都检查一次" 您的意思是不是虽然超时了,但并不会自动返回登陆页面,只有当用户提交的时候会去检查SESSION中的超时,如果超时就返回登陆页?谢谢.
如果在JS中捕捉键盘和鼠标事件,只要有触发,就把时间重置,能否解决问题?
是的,超时后,用户不操作的话,页面是不会刷新的,只有用户操作时,判断用户是否超时
其他的action都继承AbstractAction就可以了。
ActionContext类只是把几个变量封装了一下
AbstractAction代码可以参考一下:import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;import com.kmcad.ship10.exception.Ship10Exception;abstract public class AbstractAction extends Action{ public AbstractAction(){
} public abstract ActionForward execute(ActionContext actionContext)
throws Exception; public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception{
ActionForward forward = null;
ActionContext actionContext = new ActionContext(this,mapping,form,
request,response);
try{
if(mapping.getType().equals("com.kmcad.ship10.login.struts" +
".action.LoginAction") == false){
String userName = (String)request.getSession().getAttribute(
"userID");
if(userName == null || userName.trim().equals("")){
throw new Ship10Exception(
"您的连接已超时,被系统注销,请重新登陆!"); //或转向登陆页面
}
String loginTime = ToolClass.getLoginTimeByUserID(userName);
String loginTimeSession = request.getSession().getAttribute("loginTime").toString();
if(loginTime != null && !loginTime.equals("") && !loginTime.equals(loginTimeSession))
throw new Ship10Exception(
"您的连接已超时,被系统注销,请重新登陆!");
}
forward = this.execute(actionContext);
}
catch(Exception e){
e.printStackTrace();
request.setAttribute("errormessage",e);
forward = mapping.findForward("errorpage");
}
finally{}
return forward;
}
}
谢谢欧阳,humanity(总是偷窥 Java & XML)也谢谢楼主,HOHO
这样做也是没有用的,因为SESSION计算超时是判断页面没有刷新及提交动作才开始计时的,如果只是根据键盘鼠标事件将页面时间重置,等到session时间一过,还是处于超时状态的。我还一个做法,不过还没去测试,不知道可不可行,就是做一个自JSP自定义标签,标签中做一个定时器,定时检查session状态,如果超时则用pageContext.forward()转向登录页去。谢谢楼上多位兄弟回复,还有没有大虾回复,准备结贴啦:)