Tomcat 8.5.X:从Tomcat 7迁移Valve javacode

时间:2017-06-12 13:23:55

标签: java migration tomcat7 tomcat8

我需要将我们的一个自定义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>

1 个答案:

答案 0 :(得分:1)

假设您的Valve处于Context级别(必须使上述代码工作),那么您需要:

((Context) getContainer()).getManager();