Forums Register Login
controlling maxActiveSessions
I want to limit the number of sessions in Tomcat so I set the attribute maxActiveSessions for my context in server.xml. When the limit is exceeded it throws an IllegalStateException error. I want to catch this error to print an appropriate message for the user.
I had hoped that I would be able to easily handle this with with <error-page> tags in web.xml but that hasn't been the case. It seems that after the limit is exceeded Tomcat doesn't read web.xml or uses it differently somehow.
To test this I used the servlet SessionsExample that comes with Tomcat. In web.xml I added <error-page> tags for error-code 404. At first when I tried the 404 error I got the redirection that I had set in web.xml. After exceeding the maxActiveSessions limit it showed a blank page instead of the error page I had set in web.xml.
Has anyone ever had trouble with the error pages from web.xml?
thanks in advance for any and all help.
Is it possible this error is not 'catchable' within this mechanism.

The other examples are a webapp throwing the exception, but this is a case of the container *itself* throwing an exception. Just speculation.
You must be right, thank you.
Is there some way in Tomcat I can control this error message then, instead of just printing the stack trace?
Thanks again.
I just saw now that you only talk about 404.

If you're exceeding maxActiveSessions, what error does Tomcat throw? It's probably not a 404:Not Found. Possibly some 500-level error code, a variation of 'not authorized' or 'busy'.
Well I have some time on my hands today...

So I downloaded the source and took a look...So that's where your "IllegalStateException" is coming from. Interesting choice to throw a RuntimeException. This means it's unchecked and probably (certainly?) isn't checked for until "the last moment". I'm not aware of the Tomcat architecture, but at some point there must be an overall 'try / catch' block that will catch this runtime exception and simply print it out to System.err. (Do you see the "too many active sessions" at the prompt? in any log file?) Otherwise, someone attempting to exceed the max sessions would cause Tomcat to halt and blow a stack trace.

If you can find where this happens, perhaps it would be not too late to get the container to return an error code that you CAN catch for. I'll look some more myself, but that's the update so far.
[ February 18, 2003: Message edited by: Mike Curwen ]
(1 like)
Thanks for your help.
Yeah, it is an IllegalStateException sorry if I made that confusing. I only was adding the 404 to really test whether or not it was using the <error-page> tags in web.xml.
So the IllegalStateException is of course thrown when I try a getSession().
When that happened I would get this error in the browser and also in the logs: (this is just half of the stack trace)

I am able to catch the exception in my code but I am not having an easy time handling it. I was hoping there might be an easy way with tomcat. Basically I just want a page that tells the user 'too many users'. Right now it seems the only thing I can do is forward to an html page. Typically I use a jsp error page but I cannot forward to jsps because it throws this error: (again only half of the stack trace)

I�m not sure why forwarding to a JSP throws this error. The JSP is empty except for a line in the body saying �too many errors�. If I change that same JSP to an HTML it works fine.
Again thanks for the help, sorry if I am not great at explaining these problems.
My guess is that when you attempt a forward, that counts as requesting a session from the container. I haven't written JSPs in quite a while, but isn't there a way to specify that your JSP page should NOT create a new session if one doesn't exist? The default is to attempt to get the session, so that's whats probably happening when you forward.

If that doesn't work... the other thing to try is if you successfully catch the exception in your servlet, do a sendRedirect to the plain HTML page that says "too many users". You could set up which page to redirect to in your web.xml; so you can still do declarative control of where your error gets sent to.
I'm not sure why I didn't think of this before.. but I just got this to work...

within web.xml:
My servlet:

First request works great, second request gets my error page.
As for a jsp error page, I was right about JSP pages attempting to grab a session. JSPs will always attempt to provide the default objects 'application', 'session', 'request', 'response', along with a few others.

Just by placing:

<%@ page session="false" %>
At the top of your jsp error page, you'll be fine.

And also, don't make this a JSP error page. (don't say isErrorPage="true"). That's used when the thing throwing the error is a JSP itself, and this is a servlet.
Mike, thank you so much for all of your help.
You were exactly right.
What helped the most was explaining that the JSP was calling a session too. At first I didn't think my web.xml was working correctly with the <error-page> tags but that was because I was calling another JSP and it threw the IllegalStateExeption a second time.
So if my error page is an HTML (or JSP without session like you mentioned) it works perfectly, I don't even need to try and catch it in the code.
Thank you again.
I hate opening this up again but I ran into another problem with this. The solution above works for the first time you exceed the maxActiveSession limit.
For example, I have set the maxActiveSession limit at two. I tested using the code posted above for example by Mike Curwen. The first two sessions work fine. On the third I go to the servlet and the maxActiveSession limit is exceeded and throws the IllegalStateException. Then forwards to the error html page set up in my web.xml (as mentioned above), and so still it is functioning correctly. Now, if I open another browser window and go to the servlet, the IllegalStateException is thrown and the stack trace is shown in the browser, not the error page I defined.
Portion of that stack trace:

Thank you again in advance, if you have any ideas about this.
Hi Dave, I can't replicate your difficulty. Here's an update:


After using 4 windows to access the servlet once, and then refreshing each of them once, in order of opening, I get the expected output in each window, and this on the console:Also, in between stopping and starting Tomcat, I delete this file:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\ranch\SESSIONS.ser
[ February 25, 2003: Message edited by: Mike Curwen ]
thank you agin mike for all of your help.
I still do not have it working consistently. I tried the code you just posted (changed slightly the section where you throw the exception. I'm not sure what your MyException class is like, I just have 'throw ise' instead of 'throw new MyException((Throwable)ise) '.
I deleted SESSIONS.ser and I followed what you did and my output in the console is the same. The displays in browsers are different though. The first two show 'Hello World', the third shows my error page (correctly), the fourth still shows the IllegalStateException stack trace instead of the error page.
I get this with IE 6.0.
If I do a hard reload in the browser with the stack trace it then shows the error page. If i press reload it shows the stack trace.
I tested this on a few browsers.
Opera - When I press reload it alternately shows me the error page and then stack trace. So if it currently shows the stack trace and I press reload it shows the error page, press reload again and I see the stack trace again and so on.
Hard reload works the same way as with reload.
Netscape - Works like Opera with reload (alternates between stack trace and error page when you press reload) and works like IE with hard reload (shows error page every time).
Thanks again and I apologize if I have made this rather confusing.
oops, didn't even notice that. the MyException was something else I was trying. throw ise is just fine.

I just can't reproduce this condition. I've tried NS 4.7, NS 6.2, IE 5.5. The Netscape browsers I had to open and close a single window all the time, because for those, a single running instance of the browser will share sessions with multiple windows. But on the 3rd window I'd get my error page. I've even tried all 3 browsers at once, multiple refreshes. No problems.

One thing, I have all my browsers set to not cache anything at all. Could your browsers be retaining a cached copy of the "old" stack trace pages? Clear out your cache and see.
thanks as always mike, but i'm still getting the same behavior.
First two windows show 'Hello World'
Third goes correctly to my error page.
Fourth still shows the Tomcat error page with stack trace for IllegalStateException.
I turned off the caching like you suggested. Although even before that the first time the IllegalStateException is thrown it goes to the right page but the second time no.
Anyway thanks again.
I was prepared to leave this alone because I am tired of it but on a whim I tried it on an older version of Tomcat.
I have been running Tomcat 4.1.12 and decided to try Tomcat 4.0.3.
I ran the exact same test on both.
Changed maxActiveSession to 2.
Added <error-page> tags to the /example webapp web.xml page for IllegalStateException to forward to an HTML.
Run the example servlet SessionExample that comes with tomcat.
In Tomcat 4.0.3 the first two windows get sessions and every window after is forwarded to the error page correctly.
In Tomcat 4.1.12 the first two windows get sessions, the third window gets forwarded to the error page correctly and every window after shows the stack trace.
Mike what version of Tomcat are you running?
Hello All,
we are facing the same problem for which this thread was started.

We are getting a blank page when the user exceeds the maximum number of sessions allowed.
We have also included an error page as well exception tags in web.xml, but nothing seems to work.
We would want to redirect the user to an error page when he exceeds the max sessions.

The error we are getting is:

May 12 18:03:51.898530 err cla-0.internalnet.localdomain TomcatPlat[java]: May 12, 2010 6:03:51 PM org.apache.jasper.runtime.JspFactoryImpl internalGetPageContext.SEVERE: Exception initializing page context.java.lang.IllegalStateException: createSession: Too many active sessions..at org.apache.catalina.session.StandardManager.createSession(StandardManager.java:287)..at org.apache.catalina.connector.Request.doGetSession(Request.java:2312)..at org.apache.catalina.connector.Request.getSession(Request.java:2075)..at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:833)..at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:844)..at org.apache.jasper.runtime.PageContextImpl._initialize(PageContextImpl.java:146)..at org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:124)..at org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:111)..at org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:67)..at org.apache.jsp.index_jsp._jspService(index_jsp.java:44)..at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)..at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)..at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)..at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)..at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)..at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)..at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)..at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)..at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)..at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)..at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)..at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)..at org.apache.catalina.

Could someone please help us with this and any help will be greatly appreciated.

Anjali Raman
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 11320 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 20, 2018 02:23:28.