Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space IDS and version 8.2.0

...

Code Block
languagetext
themeRDarkDefault
titleIn HTML page HEAD:
<script type="text/javascript">

      function sessionKeepAlive(imgName) {
          myImg = document.getElementById(imgName);
          if(myImg) myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
      }
      window.setInterval("sessionKeepAlive('keepAliveIMG')", 600000);
</script>

...

Code Block
languagetext
themeRDarkDefault
titleInside HTML BODY:
<img id="keepAliveIMG" width="1" height="1" src="https://www.example.com/uas/refresh/status.gif?"/>

...

Code Block
languagetext
themeRDarkDefault
titleIn HTML HEAD:
<meta http-equiv="refresh" content="600" />

...

Code Block
languagetext
themeRDarkDefault
public class ImageServlet extends HttpServlet {
	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// To refresh the session
		HttpSession session = request.getSession(false);
 		ServletContext sc = getServletContext();
 		String filename = sc.getRealPath("images/empty.png");
 		String mimeType = sc.getMimeType(filename);
 		if (mimeType == null) {
 			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 			return;
 		}
		response.setContentType(mimeType);
		File file = new File(filename);
		response.setContentLength((int) file.length());
 		FileInputStream in = new FileInputStream(file);
 		OutputStream out = response.getOutputStream();
 		byte[] buf = new byte[1024];
 		int count = 0;
 		while ((count = in.read(buf)) >= 0) {
 			out.write(buf, 0, count);
 		}
 		in.close();
 		out.close();
 	}
}

...

Code Block
languagetext
themeRDarkDefault
titleIn HTML HEAD:
<script language="javascript" type="text/javascript" src="js/SessionExpiry.js"></script>
<script type="text/javascript">
var callback = function() {
// Take the user to another page just before session expiration.
document.location = "expiry.jsp";
}; 
// The time in milliseconds for the moment before actual session expiry (29 minutes)
var timeout = 1740000; 
initializeExpiryTracking(); 
// The polling interval should be much less than the session timeout
// if users open multiple windows and all the windows are wanted to
// react to the session expiry somehow. If this is not important (or
// not preferred), just set the interval the same as the timeout.
window.setInterval("checkSessionExpiry(timeout, callback)", 5000);
</script>

...

Code Block
languagetext
themeRDarkDefault
titleThe contents of SessionExpiry.js:
/**
 * Initializes the session expiration tracking by marking the last
 * http request time in a cookie.
 */
function initializeExpiryTracking() {
    setCookie("lastRequestTime", new Date().getTime());
}
/**
 * Checks if the time between now and last http request is bigger
 * than the session timeout and if so, executes the callback function.
 *
 * @param timeout Session timeout
 * @param callback a function that is called when session expiration
 *                 happens
 */
function checkSessionExpiry(timeout, callback) {
    lastRequestTimeString = getCookie("lastRequestTime");
    lastRequestTime = parseInt(lastRequestTimeString);

    if(new Date().getTime() - lastRequestTime >= timeout || cookieAlreadyDeleted()) {
        deleteCookie("lastRequestTime");
        callback();
    }
}
function cookieAlreadyDeleted() {
    return isNaN(lastRequestTime);
}

/**
 * Returns a cookie value for the given cookie name.
 *
 * @param name
 */
function getCookie(name) {
    var nameMatch = name + "=";
    var cookieArray = document.cookie.split(';');
    for(var i = 0; i < cookieArray.length; i++) {
        var c = cookieArray[i];
        while(c.charAt(0) == ' ') c = c.substring(1, c.length);
        if(c.indexOf(nameMatch) == 0) return c.substring(nameMatch.length, c.length);
    }
    return null;
}

/**
 * Sets a cookie with the given name and value. Uses default expiration and "/"
 * as path.
 *
 * @param name
 * @param value
 */
function setCookie(name, value) {
    document.cookie = name + "=" + value + ";path=/";
}

/**
 * Deletes a cookie with a given name from the "/" path.
 *
 * @param name
 */
function deleteCookie(name) {
    if(getCookie(name)) {
        document.cookie = name + "=" + ";path=/" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

/**
 * Changes the image src attribute of the image that has the given Id. This basically
 * makes the browser to reload the image.
 * 
 * @param imgName
 */
function sessionKeepAlive(imgName) {
    myImg = document.getElementById(imgName);
    if(myImg) myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
}


...

Code Block
languagetext
themeRDarkDefault
titleIn HTML HEAD section:
<script language="javascript" type="text/javascript" src="js/SessionExpiry.js"></script>
<script type="text/javascript">
var intervalId = 0;
var callback = function() {
// Here you can do whatever is wanted to be done just before session expiration.
if(confirm("Your session is about to expire. Do you want to continue the session?")) {
window.clearInterval(intervalId);
sessionKeepAlive("keepAliveIMG");
initializeExpiryTracking();
intervalId = window.setInterval("checkSessionExpiry(timeout, callback)", 5000);
} else {
document.location = "expiry.jsp";
}
};
// The time in milliseconds for the moment before actual session expiry
var timeout = 50000;
initializeExpiryTracking();
// The polling interval should be much less than the session timeout
// if users open multiple windows and all the windows are wanted to
// react to the session expiry somehow. If this is not important (or
// not preferred), just set the interval the same as the timeout.
intervalId = window.setInterval("checkSessionExpiry(timeout, callback)", 5000);
</script>

...