Search code examples
jsptomcattomcat7session-management

Tomcat disable / destroy or reuse session creation for specific requests


Background

I am using Tomcat to serve a .jsp page that can be called by a load-balancer to determine if a given server hosting an application is online and able to service requests.

The .jsp simply makes a request to the given application and to see if it is able to respond - sending an appropriate response to the load-balancer.

The issue is that each request to this .jsp is creating a session in Tomcat that persists for some time - there can potentially be many load balancers requesting this page causing a build up of sessions consuming resources.

Question

Am I able to configure Tomcat to do any of the following?

  • Not create a session to handle specific requests
  • Reuse a single session or already created session to handle specific requests
  • Immediately destroy a session that is created for specific requests, or give said session a very small lifetime

By specific requests I mean the .jsp I wish to serve from Tomcat.

I am using Tomcat 7.

Any help is appreciated!


Solution

  • You have a couple of options, here.

    First, you could simply switch to using a static file instead of a dynamic JSP page. If the server is accepting requests, this will probably work just as well as running a JSP page... unless your JSP probes certain things to ascertain application health.

    Another way is to disable session-generation for that JSP. By default, sessions are created when you access a JSP unless you specify otherwise. Here's how to do that:

    <%@page
      session="false"
      ..
    %>
    ... rest of your page
    

    Now your JSP page won't even generate a session, so you don't have to dispose of them or anything like that.

    Finally, if you need a session in that JSP for some reason, you can indeed use a single session for all clients of a particular type. You can do that using the CrawlerSessionManagerValve. Just make sure you set the crawlerUserAgents string to match whatever your load-balancer is using for its probes.