Java 替换server.xml中的Tomcat StandardContext

Java 替换server.xml中的Tomcat StandardContext,java,tomcat6,Java,Tomcat6,我正在Tomcat6.0.24上运行一个Java应用程序,它需要能够在运行时动态更改JSESSIONID cookie的路径。在决定最简单的方法可能是扩展org.apache.catalina.core.StandardContext并覆盖getEncodedPath函数之前,我花了很长时间试图在过滤器中操作cookie 我已经创建了一个名为MultiTabContext的自定义上下文,它扩展了StandardContext并删除了所有出现的类路径问题。我已经在catalina/conf/ser

我正在Tomcat6.0.24上运行一个Java应用程序,它需要能够在运行时动态更改JSESSIONID cookie的路径。在决定最简单的方法可能是扩展org.apache.catalina.core.StandardContext并覆盖getEncodedPath函数之前,我花了很长时间试图在过滤器中操作cookie

我已经创建了一个名为MultiTabContext的自定义上下文,它扩展了StandardContext并删除了所有出现的类路径问题。我已经在catalina/conf/server.xml中定义了我的上下文(我知道它应该在Context.xml中,但我将在后面处理这个问题):


因为路径是“”,所以我希望在每个请求上使用我的上下文,而不是StandardContext。但是,应用程序仍然使用StandardContext,而不是我的。任何人,都知道如何定义上下文,以便Tomcat使用我的上下文而不是StandardContext(或者知道动态更改JSESSIONID cookie路径的更好方法)?

我不确定这是否有助于回答您的问题。但是,我有一个方法可能会有所帮助。 当请求得到服务时,您可以在运行时更改JSESSIONID cookie路径

/**
 * This gets the information from the request
 * 
 * @param parameter, pass JSESSION ID 
 * @return
 */
protected String getRequestInfo(HttpServletRequest request, String parameter)
{
    Cookie[] cookies = null;
    if (request.getRequestURI() == null || request.getRequestURI().length() == 0) 
    {
        performanceLogger.debug(" Request generated null pointer");
    }
    try 
    {
        cookies = request.getCookies();
    } 
    catch (Exception ex)
    {
        cookies = null;

    }
    if (cookies == null || cookies.length == 0)
    {
        return null;
    }

    String paramValue = null;
    for (Cookie c : cookies)
    {
        if (c.getName().equals(parameter)) 
        {
            //change the path of JESSION ID over here 
            break;
        }
    }
    return paramValue;
}

这看起来与我在过滤器中尝试做的相似。我将迭代请求上的cookies,并更新JSESSIONID的路径。但是,在设置了JSESSIONID cookie的请求上,org.apache.cataline.connector.request的内部工作直接写入响应,而不更新请求上的cookie[]数组。直到下一个请求,JSESSIONID cookie才会在请求中可用,因此您最终会得到两个JSESSIONID,一个具有原始路径,另一个具有所需路径。
package foo.app.server;

import org.apache.catalina.core.StandardContext;

public class MultiTabContext extends StandardContext {

    @Override
    public String getEncodedPath() {
        return super.getEncodedPath();
    }
}
/**
 * This gets the information from the request
 * 
 * @param parameter, pass JSESSION ID 
 * @return
 */
protected String getRequestInfo(HttpServletRequest request, String parameter)
{
    Cookie[] cookies = null;
    if (request.getRequestURI() == null || request.getRequestURI().length() == 0) 
    {
        performanceLogger.debug(" Request generated null pointer");
    }
    try 
    {
        cookies = request.getCookies();
    } 
    catch (Exception ex)
    {
        cookies = null;

    }
    if (cookies == null || cookies.length == 0)
    {
        return null;
    }

    String paramValue = null;
    for (Cookie c : cookies)
    {
        if (c.getName().equals(parameter)) 
        {
            //change the path of JESSION ID over here 
            break;
        }
    }
    return paramValue;
}