aspose file tools*
The moose likes Servlets and the fly likes setMaxInactiveInterval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "setMaxInactiveInterval" Watch "setMaxInactiveInterval" New topic
Author

setMaxInactiveInterval

David CLh
Greenhorn

Joined: Sep 15, 2002
Posts: 3
Hi everybody:
My question is... Really, in method setMaxInactiveInterval(int interval), interval are seconds, minutes, ...
API says:
"Specifies the time, in seconds, between client requests before the servlet..."
Nevertheless, I have proven to put setMaxInactiveInterval(1) and setMaxInactiveInterval(60) and in both cases the session invalidates to the minute to be created.
Another question...somebody can say me that I must make to detect if a session is invalidated to redirect users to my login page??
Thanks.
Harpartap Singh
Greenhorn

Joined: Sep 14, 2002
Posts: 23
I bet $1 that you will be scolded within the next 12 hours by the "Name Police".
"your name no good"


<blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Lance Armstrong: "What am I on? I'm on my bike, busting my ass six hours a day. What are you on?"<hr></blockquote>
Harpartap Singh
Greenhorn

Joined: Sep 14, 2002
Posts: 23
And, about detecting session invalidation, you have to use the Event listener. In this case the HttpSessionListener. Write a class that implements the <b>HttpSessionListener interface</b> and override the <b>sessionDestroyed()</b> method.
Inside the sessionDestroyed() remember to call <b>HttpSessionEvent.getSession()</b> to really tell, which session was destroyed.
http://jakarta.apache.org/tomcat/tomcat-4.0-doc/servletapi/javax/servlet/http/HttpSessionListener.html
About setMaxInactiveInterval(1) or (60) behaving the same, make sure you are setting the inactive interval on the correct session. It could be using the system default, or the <session-config> <session-timeout> elements in the web.xml.
If you still can reproduce this, please post the application server/version you are using, so someone else could reproduce it.
/hs
David CLh
Greenhorn

Joined: Sep 15, 2002
Posts: 3
ok, Harpartap, as you can see, I've changed my name. I hope this is good.
I'm using Tomcat 4.0 and in web.xml file I'm using this lines:
<web-app>
<listener>
<listener-class>
SessionCounter
</listener-class>
</listener>
</web-app>
And this is my listener class:
// ...imports...
public class SessionCounter implements HttpSessionListener, Serializable {
private static int TotalSessions = 0;
private static int ActiveSessions = 0;
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
TotalSessions++;
ActiveSessions++;
session.setMaxInactiveInterval(60);
// I've prove session.setMaxInactiveInterval(1); too
registerCounter(event);
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
ActiveSessions--;
System.out.println("Session destroyed..." + session.getId());
}
public static int getTotalSessions() {
return(totalSessions);
}
public static int getActiveSessions() {
return(ActiveSessions);
}
private synchronized void registerCounter(HttpSessionEvent event) {
HttpSession session = event.getSession();
session.setAttribute("SessionCounter", this);
}
}
I think it's correct, isn't it?
When I must to send to my users toward the login page??
Thanks.
Harpartap Singh
Greenhorn

Joined: Sep 14, 2002
Posts: 23
Sorry I didn't read your question carefully. You need to detect session invalidation for "redirect"ing to login page.
Well, in that case you dont' need listeners to detect invalidation.
In the servlets that you are controlling access to, each time they are accessed (in doXXX() methods), check to see if a session exists, if not, redirect them to login servlet

Just remember a gotcha regarding sessions and JSP pages (not servlets):
- if you don't have the directive <%@ page session="false" %> in your jsp page, if a session does not exist, a NEW one will be created for you automatically.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setMaxInactiveInterval