gpt4 book ai didi

java - 使用java当系统空闲一段时间后自动注销

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:09 24 4
gpt4 key购买 nike

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

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

我尝试使用此代码作为注销选项,但是,当系统空闲 1 或 2 分钟时我需要自动注销,任何人都可以帮助我解决这个问题....

最佳答案

    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();
}
}
}

关于java - 使用java当系统空闲一段时间后自动注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com