2014-10-30 47 views
3
protected void doGet(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException { 

request.getSession().invalidate(); 
response.sendRedirect(request.getContextPath() + "/login.jsp"); 
} 

我試過註銷選項的代碼,但是,我需要自動註銷,當系統閒置1或2分鐘任何一個可以幫助我解決這個問題。 ...的Java自動註銷,當系統處於空閒狀態一段時間

+0

似乎是一個Web應用程序和系統客戶端的系統,你應該尋找一種方式來對觸發請求註銷你的服務器,並通知用戶太多的客戶端代碼的定時器。 – Vikdor 2014-10-30 04:29:59

回答

3
import java.awt.*; 
    import java.awt.event.*; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import javax.swing.*; 
class InactivityListener implements ActionListener, AWTEventListener { 
int cnt = 0; 
public final static long KEY_EVENTS = AWTEvent.KEY_EVENT_MASK; 
public final static long MOUSE_EVENTS 
     = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK; 
public final static long USER_EVENTS = KEY_EVENTS + MOUSE_EVENTS; 
private Window window; 
private Action action; 
private int interval; 
private long eventMask; 
private Timer timer = new Timer(0, this); 
public InactivityListener() throws ClassNotFoundException{ 
    Admin frame = new Admin(); 
    frame.setVisible(true); 
    Action logout = new AbstractAction() { 
     public void actionPerformed(ActionEvent e) { 
      JFrame frame = (JFrame) e.getSource(); 
      LoginForm lf = null; 
      try { 
       lf = new LoginForm(); 
      } catch (ClassNotFoundException ex) { 
       Logger.getLogger(InactivityListener.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      lf.setVisible(true); 
      frame.dispose(); 
     } 
    }; 
    InactivityListener listener = new InactivityListener(frame, logout, 1); 
    listener.start(); 
} 
public InactivityListener(Window window, Action action) { 
    this(window, action, 1); 
} 
public InactivityListener(Window window, Action action, int interval) { 
    this(window, action, interval, USER_EVENTS); 
} 
public InactivityListener(Window window, Action action, int minutes, long eventMask) { 
    this.window = window; 
    setAction(action); 
    setInterval(minutes); 
    setEventMask(eventMask); 
} 
public void setAction(Action action) { 
    this.action = action; 
} 
public void setInterval(int minutes) { 
    setIntervalInMillis(minutes * 60000); 
} 
public void setIntervalInMillis(int interval) { 
    this.interval = interval; 
    timer.setInitialDelay(interval); 
} 
public void setEventMask(long eventMask) { 
    this.eventMask = eventMask; 
} 
public void start() { 
    timer.setInitialDelay(interval); 
    timer.setRepeats(false); 
    timer.start(); 
    Toolkit.getDefaultToolkit().addAWTEventListener(this, eventMask); 
} 
public void stop() { 
    Toolkit.getDefaultToolkit().removeAWTEventListener(this); 
    timer.stop(); 
} 
public void actionPerformed(ActionEvent e) { 
    ActionEvent ae = new ActionEvent(window, ActionEvent.ACTION_PERFORMED, ""); 
    action.actionPerformed(ae); 
} 
public void eventDispatched(AWTEvent e) { 
    if (timer.isRunning()) { 
     timer.restart(); 
    } 
} 
} 
+0

我試過這個,它爲我工作。 – deathray 2014-10-30 04:31:57

0

對於Web應用程序,如果你想會話超時,那麼你可以在這裏 Session Timeout看看。這可以使用部署描述符 (即您的web.xml)中的簡單配置輕鬆完成。希望這可以幫助。

0

配置會話超時的最佳方式是在web應用程序的web.xml文件中進行配置。

<web-app ...> 
    <session-config> 
    <session-timeout>20</session-timeout> 
    </session-config> 
</web-app> 

給幾分鐘的時間。

以上設置將應用於整個Web應用程序。

或者,您可以將此設置放在您的Web服務器(Ex apache tomcat)的web.xml文件的config文件夾中,以便將其應用於服務器的所有已部署的Web應用程序。

或者您可以通過添加此代碼手動將設置應用於特定會話。

HttpSession session = request.getSession(); 
session.setMaxInactiveInterval(20*60); 
相關問題