2011-04-27 29 views
6

我需要从Tomcat中的servlet(或过滤器)访问管理器以通过自定义会话ID加载自定义会话。Tomcat:如何从servlet访问(会话)管理器

回答你的下一个问题:为什么我需要它。 Flash中存在一个旧的错误,它导致它从IE发送cookie而不是从当前浏览器发送cookie。所以,如果我在FF中,而且我试图用SWFUpload上传文件,那么最终会出现错误的会话和错误。

我想将魔术参数添加到POST,应该覆盖默认(错误)会话ID,然后加载自定义会话,而不是Tomcat加载的会话。我不能使用URL重写,因为首先解决了Cookie问题,并且当Flash从IE发送错误的Cookie时,Tomcat不会尝试从url重写的地址加载会话。

我很感激任何其他提示如何从上下文访问管理器或原始问题的解决方案。

由于提前, Juriy

回答

4

应当通过的ServletContext实施访问。获取tomcat的来源来检查,或使用反射来获取上下文的所有字段。您可能需要使用大量的反思才能找到经理。

(我找不到经理是否在JNDI暴露出来,但你可以看看有作为)

5

为Tomcat:

ApplicationContextFacade appContextFacadeObj = (ApplicationContextFacade) request.getSession().getServletContext(); 

    try 
    { 
     Field applicationContextField = appContextFacadeObj.getClass().getDeclaredField("context"); 
     applicationContextField.setAccessible(true); 
     ApplicationContext appContextObj = (ApplicationContext) applicationContextField.get(appContextFacadeObj); 
     Field standardContextField = appContextObj.getClass().getDeclaredField("context"); 
     standardContextField.setAccessible(true); 
     StandardContext standardContextObj = (StandardContext) standardContextField.get(appContextObj); 
     Manager persistenceManager = standardContextObj.getManager(); 
    } 
    catch(SecurityException e) 
    { 
     logger.error(e); 
    } 
    catch(NoSuchFieldException e) 
    { 
     logger.error(e); 
    } 
    catch(IllegalArgumentException e) 
    { 
     logger.error(e); 
    } 
    catch(IllegalAccessException e) 
    { 
     logger.error(e); 
    } 
6

与之相对伊霍尔的代码,这段代码使用少一点的抽象从HttpSession

private Manager manager(HttpSession session) throws Exception { 

    Field facadeSessionField = StandardSessionFacade.class.getDeclaredField("session"); 
    facadeSessionField.setAccessible(true); 
    StandardSession stdSession = (StandardSession) facadeSessionField.get(session); 

    return stdSession.getManager(); 
} 
+0

这真的救了我的培根,谢谢! – RTF 2016-05-22 20:46:51