2012-04-16 1 views
1

私はクッキーを使用しています。あなたは私はあなたがページを初めて開いたときは、条件if (sessionID == null && !sessionValid)が真になるとuserCookieは、その後、あなたはブラウザのタブを閉じた場合normal.Nowを値に設定し、このCookieの値が変更されていません。前回の値を取得するたびに

public class SessionTimeoutFilter implements Filter { 

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) 
     throws IOException, ServletException { 

     Object isRegistered = session.getAttribute("logedin"); 
     if (isRegistered != null) { 
      String isRegisteredUser = isRegistered.toString(); 
      if (isRegisteredUser.equalsIgnoreCase(("1"))) { 
       sessionID = UUID.randomUUID().toString();    
       session.setMaxInactiveInterval(240); //4min 
       Cookie userCookie = getCookie(httpServletRequest, "userCookie"); 
       if (userCookie != null) { 

         //Value not setting here 
         Cookie loginUserCookie = new Cookie("userCookie", "loginUser"); 
         httpServletResponse.addCookie(loginUserCookie); 
       } 
       filterChain.doFilter(httpServletRequest, httpServletResponse); 
      } 
     } else { 
      sessionID = httpServletRequest.getRequestedSessionId(); 
      sessionValid = httpServletRequest.isRequestedSessionIdValid(); 

      //User open his browser 
      if (sessionID == null && !sessionValid) { 
       sessionID = UUID.randomUUID().toString(); 
       Cookie browserCookie = new Cookie("browserCookie", sessionID); 
       httpServletResponse.addCookie(browserCookie); 
       Cookie userCookie = new Cookie("userCookie", "normal"); 
       httpServletResponse.addCookie(userCookie); 

       session.setAttribute("logedin", "0"); 
       filterChain.doFilter(httpServletRequest, httpServletResponse); 

      //Session expires. Each time user close the tab and session expires automatically 
      } else if(sessionID != null && !sessionValid) { 
       if (httpServletRequest.isRequestedSessionIdFromCookie()) { 
        Cookie userCookie = getCookie(httpServletRequest, "userCookie"); 
        String value = userCookie.getValue(); 

        //Each time getting normal 
        if (value.equalsIgnoreCase("normal")) { 
         session.setAttribute("logedin", "0"); 
         filterChain.doFilter(httpServletRequest, httpServletResponse); 
        } else if (value.equalsIgnoreCase("loginUser")) { 

        } 
       } //end of if (httpServletRequest.isRequestedSessionIdFromCookie()) 
      } 
     } 
    } //end of dofilter() 
} //end of class SessionTimeoutFilter 

のようにクッキーを設定するページを初めて開いたときページを再度開きます。クッキーの値は正常です。 OK。

これでログインすると、条件if (isRegisteredUser.equalsIgnoreCase(("1")))になります。ここで私はuserCookieの値を置き換えようとしています。

Cookie userCookie = getCookie(httpServletRequest, "userCookie"); 

      if (userCookie != null) { 

       String value = userCookie.getValue(); 

       //delete the cokie 
       //userCookie.setValue("loginUser"); 
       //userCookie.setMaxAge(0); 

       Cookie loginUserCookie = new Cookie("userCookie", "loginUser"); 
       httpServletResponse.addCookie(loginUserCookie); 

      } 

しかし両方の方法は機能しません。今私は、ブラウザを閉じて、再度ページを開く場合、私ので、条件

if (value.equalsIgnoreCase("normal")) { 

    session.setAttribute("logedin", "0"); 
    filterChain.doFilter(httpServletRequest, httpServletResponse); 

} else if (value.equalsIgnoreCase("loginUser")) { 

} 

に私は、userCookie value..This時間として、それは私に値「loginUser」を取得する必要があり、再び正常な取得していますことをやった後、 userCookieの値をloginUserに変更しました。私はここで正常になっている。なぜ私は以前の値を取得しています。私は間違って何をしていますか?教えてください。

おかげ

答えて

4

は、クッキーのパスを設定し、クッキーのパスを設定した後

Cookie userCookie = new Cookie("userCookie", "loginUser"); 
userCookie.setPath("/"); 
httpServletResponse.addCookie(userCookie); 

のような問題を解決するため、値が置き換えられます。

ありがとうございました

0

あなたの場合、クッキーは有効期限または定義された最大年齢を持っていない - それは、ユーザがブラウザを閉じると自動的に削除されますセッションクッキー、と考えられています。 http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age

クッキーの有効期限を設定する必要があります。参照:http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/Cookie.html#setMaxAge(int)

  Cookie loginUserCookie = new Cookie("userCookie", "loginUser"); 
      loginUserCookie.setMaxAge(3600); // cookie expires in an hour 
      httpServletResponse.addCookie(loginUserCookie); 
+0

ok、セッションレベルのクッキーです。しかし、もし私がセッションレベルのクッキーの価値を変更したいのであればどうでしょうか?私のセッションレベルのクッキーは、ユーザーがログインしたときに、セッションレベルのクッキーの値を変更したい場合、通常の値を持っています。クッキーの価値を変更したいときは、クッキーの有効期限を設定する必要はありますか?ありがとう – Basit

+0

ちなみに、HTTPトラフィックモニタ(Firefox用のFireBugプラグインなど)を使用して、アプリケーション内でどのCookieが設定/転送されているかを確認できます。 – DRCB