httprequest.getsession返回null [英] httprequest.getsession is returning null

查看:1125
本文介绍了httprequest.getsession返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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 {


    }

}         

推荐答案

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

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

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

如果要在使当前会话无效之后创建新会话,请使用:request.getSession(true)或更简单的方式:request.getSession().

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

这篇关于httprequest.getsession返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆