CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-tomcat-embed--tomcat-embed-core

Embedded Apache Tomcat servlet container with Jakarta Servlet API, HTTP connectors, and lifecycle management for Java web applications

Overview
Eval results
Files

session-management.mddocs/

Session Management

HTTP session tracking and management with configurable persistence stores, session ID generation, timeout handling, clustering support, and event notification. Includes support for standard in-memory sessions, persistent file-based sessions, and JDBC-based session storage.

Capabilities

Manager Interface

Core session manager interface for managing HTTP sessions.

public interface Manager {
    // Context
    Context getContext();
    void setContext(Context context);
    
    // Session ID generator
    SessionIdGenerator getSessionIdGenerator();
    void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator);
    
    // Session operations
    void add(Session session);
    Session createSession(String sessionId);
    Session createEmptySession();
    Session findSession(String id) throws IOException;
    Session[] findSessions();
    void remove(Session session);
    void remove(Session session, boolean update);
    void changeSessionId(Session session, String newId);

    // Configuration
    int getMaxInactiveInterval();
    void setMaxInactiveInterval(int interval);
    
    // Statistics
    int getActiveSessions();
    long getExpiredSessions();
    void setExpiredSessions(long expiredSessions);
    int getRejectedSessions();
    long getSessionCounter();
    void setSessionCounter(long sessionCounter);
    int getMaxActive();
    void setMaxActive(int maxActive);
    int getSessionMaxAliveTime();
    void setSessionMaxAliveTime(int sessionMaxAliveTime);
    int getSessionAverageAliveTime();
    int getSessionCreateRate();
    int getSessionExpireRate();
    
    // Persistence
    void load() throws ClassNotFoundException, IOException;
    void unload() throws IOException;
    
    // Background processing
    void backgroundProcess();
    
    // Will support persistence
    boolean willAttributeDistribute(String name, Object value);

    // Session ID rotation
    default String rotateSessionId(Session session);

    // Listener notification configuration
    default boolean getNotifyBindingListenerOnUnchangedValue();
    void setNotifyBindingListenerOnUnchangedValue(boolean notifyBindingListenerOnUnchangedValue);
    default boolean getNotifyAttributeListenerOnUnchangedValue();
    void setNotifyAttributeListenerOnUnchangedValue(boolean notifyAttributeListenerOnUnchangedValue);

    // Session activity tracking
    default boolean getSessionActivityCheck();
    void setSessionActivityCheck(boolean sessionActivityCheck);
    default boolean getSessionLastAccessAtStart();
    void setSessionLastAccessAtStart(boolean sessionLastAccessAtStart);

    // Event listeners
    void addPropertyChangeListener(PropertyChangeListener listener);
    void removePropertyChangeListener(PropertyChangeListener listener);
}

Session Interface

Internal representation of an HTTP session.

public interface Session {
    // Session ID
    String getId();
    String getIdInternal();
    void setId(String id);
    void setId(String id, boolean notify);
    
    // Manager
    Manager getManager();
    void setManager(Manager manager);
    
    // Timing
    long getCreationTime();
    long getCreationTimeInternal();
    void setCreationTime(long time);
    long getLastAccessedTime();
    long getLastAccessedTimeInternal();
    long getThisAccessedTime();
    long getThisAccessedTimeInternal();
    long getIdleTime();
    long getIdleTimeInternal();
    int getMaxInactiveInterval();
    void setMaxInactiveInterval(int interval);
    
    // State
    void setNew(boolean isNew);
    boolean isValid();
    void setValid(boolean isValid);
    
    // Lifecycle
    void access();
    void endAccess();
    void expire();
    void recycle();
    
    // Attributes
    Object getNote(String name);
    void setNote(String name, Object value);
    void removeNote(String name);
    Iterator<String> getNoteNames();
    
    // Authentication
    Principal getPrincipal();
    void setPrincipal(Principal principal);
    String getAuthType();
    void setAuthType(String authType);
    
    // HTTP session
    HttpSession getSession();

    // Session ID change notification
    void tellChangedSessionId(String newId, String oldId, boolean notifySessionListeners, boolean notifyContainerListeners);

    // Attribute distribution check
    boolean isAttributeDistributable(String name, Object value);

    // Event constants
    String SESSION_CREATED_EVENT = "createSession";
    String SESSION_DESTROYED_EVENT = "destroySession";
    String SESSION_ACTIVATED_EVENT = "activateSession";
    String SESSION_PASSIVATED_EVENT = "passivateSession";

    // Event listeners
    void addSessionListener(SessionListener listener);
    void removeSessionListener(SessionListener listener);
}

Session ID Generator

Interface for generating session IDs.

public interface SessionIdGenerator {
    // JVM route (for clustering)
    String getJvmRoute();
    void setJvmRoute(String jvmRoute);
    
    // Session ID length
    int getSessionIdLength();
    void setSessionIdLength(int sessionIdLength);
    
    // Generation
    String generateSessionId();
    String generateSessionId(String route);
}

Store Interface

Persistent session storage.

public interface Store {
    // Manager
    Manager getManager();
    void setManager(Manager manager);
    
    // Size
    int getSize() throws IOException;
    
    // Operations
    String[] keys() throws IOException;
    Session load(String id) throws ClassNotFoundException, IOException;
    void remove(String id) throws IOException;
    void clear() throws IOException;
    void save(Session session) throws IOException;
    
    // Event listeners
    void addPropertyChangeListener(PropertyChangeListener listener);
    void removePropertyChangeListener(PropertyChangeListener listener);
}

Usage Examples

Custom Session Configuration

import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.Context;
import org.apache.catalina.Manager;
import org.apache.catalina.session.StandardManager;

public class SessionConfigExample {
    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        
        Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
        
        // Configure session manager
        StandardManager manager = new StandardManager();
        manager.setMaxActiveSessions(1000);
        manager.getSessionIdGenerator().setSessionIdLength(32);
        ctx.setManager(manager);
        
        // Configure session timeout (minutes)
        ctx.setSessionTimeout(30);
        
        tomcat.start();
        tomcat.getServer().await();
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-tomcat-embed--tomcat-embed-core

docs

authentication.md

catalina-core.md

connectors.md

embedded-tomcat.md

index.md

logging.md

servlet-api.md

session-management.md

utilities.md

valves.md

web-resources.md

tile.json