session time out in jquery+ajax

5677 단어 session
Step 1
timeout.js
(function($){
 $.fn.idleTimeout = function(options) {
    var defaults = {
            inactivity: 1500000, //25 Minutes
            noconfirm: 10000, //10 Seconds
            sessionAlive: 30000, //10 Minutes
            redirect_url: '/view/LoginError?errorCode=3',
            click_reset: true,
            alive_url: '/view/KeepSessionAlive',
            logout_url: '/view/LoginError?errorCode=3'
        }
  
  
    //##############################
    //## Private Variables
    //##############################
    var opts = $.extend(defaults, options);
    var liveTimeout, confTimeout, sessionTimeout;
    var initialTimer;
    var modal = "<div id='modal_pop'><p>You will be logged out of Expert AdminSpace in <span>5:00</span> minutes. </p></div>";
    //##############################
    //## Private Functions
    //##############################
    function setTimer(){
        var sec = 300;
           var timer = setInterval(function() {
               sec--;
               var m = Math.floor(sec / 60);
               var s = Math.floor(sec % 60);
              $('#modal_pop span').text(
                      (m > 0 ? m + ' minute' + ((m > 1) ? 's ' : ' ') : '') +
                   s + ' second' + ((s > 1) ? 's' : ''));
           }, 1000);
   }
    var start_liveTimeout = function()
    {
      clearTimeout(liveTimeout);
      clearTimeout(confTimeout);
      liveTimeout = setTimeout(logout, opts.inactivity);
     
      if(opts.sessionAlive)
      {
        clearTimeout(sessionTimeout);
        sessionTimeout = setTimeout(keep_session, opts.sessionAlive);
      }
    }
   
    var logout = function()
    {
     
      confTimeout = setTimeout(redirect, opts.noconfirm);
      initialTimer = setTimer();
      $(modal).dialog({
        buttons: {"Stay Logged In":  function(){
          $(this).dialog('close');
          stay_logged_in();
        }},
        modal: true,
        title: 'Expert AdminSpace'
      });
     
    }
   
    var redirect = function()
    {
      if(opts.logout_url)
      {
        $.get(opts.logout_url);
      }
      window.location.href = opts.redirect_url;
    }
   
    var stay_logged_in = function(el)
    {
      start_liveTimeout();
      if(opts.alive_url)
      {
        $.get(opts.alive_url);
      }
    }
   
    var keep_session = function()
    {
      $.get(opts.alive_url);
      clearTimeout(sessionTimeout);
      sessionTimeout = setTimeout(keep_session, opts.sessionAlive);
    }
   
    //###############################
    //Build & Return the instance of the item as a plugin
    // This is basically your construct.
    //###############################
    return this.each(function() {
      obj = $(this);
      start_liveTimeout();
      if(opts.click_reset)
      {
        $(document).bind('click', start_liveTimeout);
      }
      if(opts.sessionAlive)
      {
        keep_session();
      }
    });
   
 };
})(jQuery);


step 2:
in web.xml

<servlet>
  <servlet-name>KeepSessionAlive</servlet-name>
  <servlet-class>com.filogix.expert.view.uam.KeepSessionAlive</servlet-class>
  </servlet>



    <servlet-mapping>
    <servlet-name>KeepSessionAlive</servlet-name>
    <url-pattern>/view/KeepSessionAlive</url-pattern>
  </servlet-mapping>


step 3

package com.filogix.expert.view.uam;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import com.filogix.expert.view.ViewServlet;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: </p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class KeepSessionAlive
  extends ViewServlet {
    public static final Logger log = Logger.getLogger(KeepSessionAlive.class);
        //Clean up resources
        public void destroy() {
      }

    @Override
    public void processRequest(HttpServletRequest request,
            HttpServletResponse response, int method) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        try {
            response.setContentType("text");
            response.setHeader("Cache-Control", "no-cache");
            response.getWriter().write("Y");
             
            } catch (Throwable t){
                log.error(t);
            }
           
    }
}

좋은 웹페이지 즐겨찾기