我需要将我们的一个自定义 Valve 代码从 Tomcat 7 迁移到 Tomcat 8.5:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Container;
import org.apache.catalina.Manager;
import org.apache.catalina.Session;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
public class SessionManagerValve extends ValveBase {
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
// ...
}
public HttpSession findSession(String id, boolean updateLastAccessTime) {
try {
Container container = getContainer();
// works with Tomcat 7.x, but not with Tomcat 8.5 anymore...
Manager man = container.getManager();
Session sess = man.findSession(id);
return sess.getSession();
} catch (Exception e) {
return null;
}
}
}
Container
接口不再提供getManager()
方法。根据Tomcat迁移指南,访问Manager
已从Container
转移到Context
。
任何人都可以给我一个提示,如何从ValveBase
对象中访问Manager
?
提前感谢, 雷纳
编辑:上下文配置是 - 如答案中正确指出的那样 -
<Context path="" ...>
<Valve className="SessionManagerValve" />
</Context>
假设您的Valve
处于Context
级别(必须为上面的代码工作(,那么您需要:
((Context) getContainer()).getManager();