request.getSession(false)没有检测到前一个会话

问题描述:

我有三个不同的服务器运行Tomcat 7.我使用JDK 7和Spring框架。request.getSession(false)没有检测到前一个会话

在我的开发和生产环境中,一切都很顺利,但是在第三台机器上,我遇到了检测会话的问题。

在第三台服务器上,初始页面打开正常,但以下代码行返回null。

@RequestMapping(value="/getCaptcha",method=RequestMethod.POST) 
    public @ResponseBody OutputStream getCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws IOException{ 

    String stToken = request.getParameter("token"); 
    OutputStream os = null; 

    try{ 
     HttpSession httpsession = request.getSession(false); 
     System.out.println("HttpSession: "+httpsession); 
     if(httpsession != null){ 
      ... 
     } 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 

    return os; 
} 

这里HttpSession中为空,它不能检测我以前用

HttpSession httpsession = request.getSession(true); 
从这个

除了设置会话,在我的整个项目request.getSession(false)的返回null即使会议之前设置。

我不确定是否遗漏了一些东西,因为相同的代码在其他两台服务器上完美运行。任何帮助/指导/建议将不胜感激。

+0

只要尝试request.getSession()来获取会话。 – user1211

+0

@ user1211如果没有会话存在,request.getSession()将创建一个新的会话。这不是他问的。 – Chinmay

+0

@Chinmay是正确的,我试图让我现有的会话。 –

你可以简单地在Spring MVC

@RequestMapping("/signup") 
public void handleMyRequest(HttpSession session, ...) { 
    ... 
} 

不知道,请求方法声明HTTP会话是否适合你的要求或没有。我为我的项目做了这个。

+0

感谢您的回应,我还没有尝试过您建议的解决方案,但是我的项目已经拥有数千种方法,取代每种方法的签名都不是一个可行的解决方案。我想弄清楚在第三台服务器上丢失了什么配置/设置,这会阻止相同的代码在前两台服务器上正常工作的情况下正确执行。 –