Search code examples
jsftomcattomcat8httpsession

Tomcat 8 creates a new session on startup


I just noticed today that every time Tomcat(8) starts up, it creates a new HttpSession (without any HttpServletRequest).

I just added a SessionListener like this:

public class SessionListener implements HttpSessionListener {
    public SessionListener() {}

    public void sessionCreated(HttpSessionEvent sessionEvent) {
        HttpSession session = sessionEvent.getSession();
        ServletContext context = session.getServletContext();

        try {
            if(session.isNew()){
                System.out.println("a new Session is created");
            }
        } catch (Exception e) {}
    }

    public void sessionDestroyed(HttpSessionEvent sessionEvent) {}
}

The only thing I changed today is this in the context.xml:

<Context>
    <Resource name="jdbc/test"
              auth="Container"
              type="javax.sql.DataSource"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"           
              maxActive="-1"
              minIdle="-1"
              maxWait="10000"
              initialSize="10"
              username="XYZ" 
              password="XYZ" 
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/test"/>
</Context>

But the last couldn't be the cause of creation of a session on container startup, right?


Solution

  • This will happen when you're running the server from inside an IDE like Eclipse. The IDE's server plugin such as Eclipse Tomcat plugin may after the startup process perform a self-test by sending a GET request to / (so the IDE server plugin can mark server as "Started"). Apparently you've on / a page which (implicitly) creates a new session.

    Ignore it. This won't happen during production.