这个在网页文件中加上每次刷新都去强制去服务器检查
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",0);

解决方案 »

  1.   

    我们做过这样的一个东西。。
    也是超时的话,转到登陆页面。
    你要做个监听,对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) {    }
    }
      

  2.   

    感谢 lakers530(欧阳) 的回贴,虽然我不是楼主:p 
    可是从你的代码中只能看出能自行删除了数据库中的相应记录,并不能看出是如何自动返回登陆页面的呀?谢谢.
      

  3.   

    <head>
    <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  -->
      

  4.   

    多谢各位的回复,不过感觉楼上humanity 的方法比较可行,但是自动刷新是不可以的,如果自动刷新页面,session将重新开始计算超时时间,这样不等于怎么也不会超时了:)
    但这样感觉还是有问题,我如果只是在页面上输入一些东西没有提交,这样页面并没有刷新,但是我是有在页面上做事情的,session也不会计超时,那我不刷新页面,这个提醒对话框到了25分钟不管你有没有在页面上响应都会弹出来,是不是会这样??楼上的兄弟
      

  5.   

    给session设置周期,然后这个值一样也给javascript,让javascript把这个时间慢慢减成0,然后弹出超时对话框
      

  6.   

    回复 rover11(外科主任)
    我们用的是struts,在action中封装了一层,在上层中检验session中的值是否已经删除即是否超时。超时的话就在上层类中转向登陆页面。如果没有超时的话,就执行他的子类,
    即用户每操作一次都检查一次
      

  7.   

    lakers530(欧阳)
    啊.......太好了,我想知道的就是哪个action的父类中相关功能是如何实现的?附:引:"即用户每操作一次都检查一次" 您的意思是不是虽然超时了,但并不会自动返回登陆页面,只有当用户提交的时候会去检查SESSION中的超时,如果超时就返回登陆页?谢谢.
      

  8.   

    回复( xiyiren(深蓝))
    如果在JS中捕捉键盘和鼠标事件,只要有触发,就把时间重置,能否解决问题?
      

  9.   

    回复 rover11(外科主任)
    是的,超时后,用户不操作的话,页面是不会刷新的,只有用户操作时,判断用户是否超时
    其他的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;
        }
    }
      

  10.   

    明白.
    谢谢欧阳,humanity(总是偷窥 Java & XML)也谢谢楼主,HOHO
      

  11.   

    回复 欧阳:你的做法也没有实现超时‘自动’弹出提示对话框,只是在用户提交时去做检查。回复 rover11(外科主任) 如果在JS中捕捉键盘和鼠标事件,只要有触发,就把时间重置,能否解决问题?
    这样做也是没有用的,因为SESSION计算超时是判断页面没有刷新及提交动作才开始计时的,如果只是根据键盘鼠标事件将页面时间重置,等到session时间一过,还是处于超时状态的。我还一个做法,不过还没去测试,不知道可不可行,就是做一个自JSP自定义标签,标签中做一个定时器,定时检查session状态,如果超时则用pageContext.forward()转向登录页去。谢谢楼上多位兄弟回复,还有没有大虾回复,准备结贴啦:)
      

  12.   

    把SESSION的时间给JAVASCRIPT,然后用JAVASCRIPT计算时间然后报出错误或者转页