By using java auto logout when the system has been idle for a while

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

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

      

I tried this code for the logout option, but I need an automatic logout when the system is idle for 1 or 2 minutes, can anyone help me solve this problem ....

+3


source to share


3 answers


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

      



+3


source


For web applications, if you need a session timeout, you can look at Session Timeout here . This can be easily done using a simple configuration in the deployment descriptor i.e. yours web.xml

. Hope this helps.



0


source


The best way to customize the session time is by setting in your web application's web.xml file.

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

      

specify the time in minutes.

the above settings will be applied to the entire web application.

Or you can put this setting in your web server's web.xml file (Ex apache tomcat) in the config folder so that it is applied to the entire deployed server web application.

Or you can manually apply the settings to a specific session by adding this code.

HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);

      

0


source







All Articles