我需要将我们的一个自定义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
。
有人可以提示我如何从Manager
对象中访问ValveBase
吗?
提前致谢, 赖
编辑:上下文配置 - 正如答案中正确指出的那样 -
<Context path="" ...>
<Valve className="SessionManagerValve" />
</Context>
答案 0 :(得分:1)
假设您的Valve
处于Context
级别(必须使上述代码工作),那么您需要:
((Context) getContainer()).getManager();