1. 程式人生 > >Tomcat自定義Session管理

Tomcat自定義Session管理

package com.nosession;  

import java.io.IOException;  
import org.apache.catalina.Lifecycle;  
import org.apache.catalina.LifecycleException;  
import org.apache.catalina.LifecycleListener;  
import org.apache.catalina.LifecycleState;  
import org.apache.catalina.Session;  
import org.apache.catalina.session.ManagerBase;  
import
org.apache.catalina.util.LifecycleSupport; public class SessionManager extends ManagerBase implements Lifecycle { protected LifecycleSupport lifecycle = new LifecycleSupport(this); @Override public void addLifecycleListener(LifecycleListener listener) { lifecycle.addLifecycleListener(listener); } @Override
public LifecycleListener[] findLifecycleListeners() { return lifecycle.findLifecycleListeners(); } @Override public void removeLifecycleListener(LifecycleListener listener) { lifecycle.removeLifecycleListener(listener); } @Override protected
synchronized void startInternal() throws LifecycleException { super.startInternal(); setState(LifecycleState.STARTING); setDistributable(true); } @Override protected synchronized void stopInternal() throws LifecycleException { setState(LifecycleState.STOPPING); } @Override public int getRejectedSessions() { return 0; } public void setRejectedSessions1(int i) { } @Override public void load() throws ClassNotFoundException, IOException { } public void setRejectedSessions(int arg0) { } @Override public void unload() throws IOException { } @Override public Session createSession(String sessionId) { // TODO 關鍵位置 return null; } public Session createSession() { // TODO 關鍵位置 return null; } @Override public Session createEmptySession() { // TODO 關鍵位置 return null; } @Override public void add(Session session) { } @Override public Session findSession(String id) { return null; } @Override public void remove(Session session) { remove(session, false); } @Override public void remove(Session session, boolean update) { } @Override public void processExpires() { } }

用該程式碼單獨建立一個工程,並匯出為jar檔案,並把匯出的jar檔案放到Tomcat的lib資料夾中,然後修改配置檔案context.xml,修改環境Manager節點為如下內容:

<Manager className="com.nosession.SessionManager" />