How can I disable resetting session timeout in Tomcat 5.5 for particular requests? On a page I have a javascript function that periodically sends ajax calls to the server. On server side I don't want these calls to renew session lifetime.

Thank you.

有帮助吗?

解决方案

Ok, since you didn't like my first idea, I came up with this JSP demonstration. It's kind of a hack but it does work. To test, copy and paste. Browse to the first JSP. It will start a session and set the inactive interval. After being redirected, keep hitting the Refresh button on your browser. No matter how times you request the second JSP, the current session will die.

test1.jsp

<%
   session.setMaxInactiveInterval(20); //for easy testing
   response.sendRedirect("test2.jsp");
%>

test2.jsp

<%@ page session="false" import="java.util.*" %>
<%
  HttpSession session = request.getSession(false);
  if(session == null){
                  out.print("Error, No Session!");
                  return;
  }
  long creationTime = session.getCreationTime();
  long now = new Date().getTime();
  long lastAccessedTime = session.getLastAccessedTime();
  int oldInterval = session.getMaxInactiveInterval();
  int inactivePeriod = (int)(now - lastAccessedTime)/1000; 
  session.setMaxInactiveInterval(oldInterval - inactivePeriod); 
  int newInterval = session.getMaxInactiveInterval();
%>
<html>
<body>
session id is <%=session.getId()%>
<br/><%=creationTime%> = creationTime
<br/><%=lastAccessedTime%> = lastAccessedTime
<br/><%=now%> = now
<br/><%=oldInterval%> = oldInterval in seconds 
<br/><%=inactivePeriod%> = inactivePeriod
<br/><%=newInterval%> = newInterval in seconds 
</body>
</html>

其他提示

I would create a separate web app(along side your present one) to take the AJAX calls. In this second web app, you can avoid creating sessions. You can do this by using session="false" in the page diective of your JSP or by just not creating one explicitly in a Servlet. If you need to share data between these two apps then you can use the attribute crossContext="true" in your Context definition. Tomcat Docs

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top