java httprequest.getsession 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25664811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:25:29  来源:igfitidea点击:

httprequest.getsession is returning null

javasecuritytomcat6tomcat5

提问by aru

I have written one filter which would invalidate current session and create new session and copy attributes of old session into new session. This is working fine in tomcat5 and jdk 1,4 but when i switched it to tomcat6 and jdk 1.6 once the filter is running then for next request httprequest.getsession(false )is null. Why its behaving differently in tomcat 6 . Please help. here is the code snippet

我编写了一个过滤器,它会使当前会话无效并创建新会话并将旧会话的属性复制到新会话中。这在 tomcat5 和 jdk 1,4 中工作正常,但是当我将它切换到 tomcat6 和 jdk 1.6 时,一旦过滤器正在运行,那么下一个请求httprequest.getsession(false )null. 为什么它在 tomcat 6 中的行为不同。请帮忙。这是代码片段

public void doFilter(ServletRequest request, ServletResponse response,
                        FilterChain chain) throws IOException, ServletException 
                        { 
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        System.out.println("within GenerteNewSessionFilter");
        System.out.println("within GenerteNewSessionFilter getRequestURI"+httpRequest.getRequestURI());
        System.out.println("within GenerteNewSessionFilter getRequestURL"+httpRequest.getRequestURL());

        System.out.println("within GenerteNewSessionFilter session false"+httpRequest.getSession(false));

        String reqUrl=httpRequest.getRequestURL().toString();
        if (reqUrl==null){
            reqUrl="";
        }
        int idxLogin=reqUrl.indexOf("LoginSubmit.jsp");
        int idxReg=reqUrl.indexOf("RegistrationSubmit.jsp");
        int idxErr=reqUrl.indexOf("Error");

    int idxSave=-1;
    System.out.println("within GenerteNewSessionFilter reqUrl "+reqUrl);
    System.out.println("within GenerteNewSessionFilter idxLogin index"+idxLogin);
    System.out.println("within GenerteNewSessionFilter idxReg index"+idxReg);
    System.out.println("within GenerteNewSessionFilter idxErr index"+idxErr);

    if (httpRequest.getSession(false) != null  &&  (idxLogin >0 || idxReg >0) && idxErr <0 ){
      //copy session attributes from old session to a map. 
        System.out.println("copy session attributes from old session to a map");
        HttpSession session = httpRequest.getSession();

      HashMap old=new HashMap();
      Enumeration keys = (Enumeration) session.getAttributeNames();
      while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();

       old.put(key, session.getAttribute(key));
          session.removeAttribute(key);


      }
      System.out.println("old  session id  "+session.getId());

      //invalidate session and create new session.
      session.invalidate();
      //create new session
      session = httpRequest.getSession(true);

      //copy session attributes from map session to new session
      Iterator it = old.entrySet().iterator(); //iterator
      while (it.hasNext()) {
          Map.Entry pairs = (Map.Entry)it.next();
          //putitng old attributes in to new session
          session.setAttribute((String) pairs.getKey(), pairs.getValue());

      }//end while loop
          System.out.println("end copy  session attributes");
          System.out.println("new session id status "+httpRequest.getSession(false));
          System.out.println("final new   session session id  "+session.getId());
    }
    chain.doFilter(request, response);
    }
      public void init(FilterConfig filterConfig) throws ServletException {


    }

}         

回答by icza

The javadoc of HttpServletRequest.getSession(boolean create)clearly states that if you pass a falsevalue to its parameter, it will only return the HttpSessionif there is one existing already. If there is no session associated with the request, it will return null.

HttpServletRequest.getSession(boolean create)的 javadoc明确指出,如果您将一个false值传递给其参数,它只会在HttpSession已经存在的情况下返回。如果没有与请求关联的会话,它将返回null

So if you invalidate your current session, obviously calling request.getSession(false)in your next request will return null.

因此,如果您使当前会话无效,显然调用request.getSession(false)您的下一个请求将返回null.

If you want to create a new session after invalidating the current one, then use: request.getSession(true)or more simply: request.getSession().

如果您想在使当前会话无效后创建一个新会话,请使用:request.getSession(true)或更简单地说: request.getSession()

回答by Eshwar Iyer

I faced a similar issue in 7.0.82. In 7.0.57the httpRequest.getSession(false)returned a session (the session was already created), but in 7.0.82, it was returning null. Absolutely no code change, only the tomcat was upgraded.

我在7.0.82 中遇到了类似的问题。在7.0.57httpRequest.getSession(false)返回一个会话(会话已经创建),但在 7.0.82 中,它返回 null。完全没改代码,只升级了tomcat。

I was able to resolve this by adding the JsessionId cookie manually.

我能够通过手动添加 JsessionId cookie 来解决这个问题。

HttpSession session = request.getSession(false);
Cookie userCookie;
if (request.getParameter("JSESSIONID") != null) {
    userCookie = new Cookie("JSESSIONID", 
    request.getParameter("JSESSIONID"));
} else {
String sessionId = session.getId();
userCookie = new Cookie("JSESSIONID", sessionId);
}
response.addCookie(userCookie);