2011-07-04 46 views
0

我有一些問題想代碼登錄+餅乾..當用戶登錄我創建會話getThreadLocalRequest()的getSession(假)總是空

getThreadLocalRequest().getSession(true) 

,當我要檢查,如果會議的還活着總是返回null

HttpSession session = getThreadLocalRequest().getSession(false);  
     if (session != null) { 
      } 

當我檢查的HttpRequest(當我檢查會話有效),這有

Cookie: JSESSIONID=a1042a8e-9ebc-45b8-a3d8-12161885be96 

和cookie是好的。

我使用Eclipse +發展模式

服務器端代碼:

public String login(String rut, String pass) throws AuthenticationException { 
    //if UserPassMatch ... 

session = this.getThreadLocalRequest().getSession(true); 
    //set user bean 
session.setAttribute("beanSession", us); 

HttpServletResponse response = getThreadLocalResponse(); 
Cookie usernameCookie = new Cookie("JSESSIONID", us.getSessionID()); 
usernameCookie.setPath("/"); 
usernameCookie.setMaxAge(60 * 60); //1 hora 
response.addCookie(usernameCookie); 


} 

@Override 
public String checkIfSessionStillActive(String token) { 

HttpServletRequest request = getThreadLocalRequest(); 
    //Here ALWAYS return null 
HttpSession session = request.getSession(false); 

if (session != null) { 
     //check sessionId and search User Bean 
} 

    return token; 
} 

從客戶端沒有什麼特別的只是調用checkIfSessionStillActive進行檢查,如果存在會話,去扔令牌或去登錄,如果不是。當用戶登錄時仍然返回會話NULL。我使用MVP模式,我從AppController調用,並使用相同的rpcService。一旦用戶登錄我檢查會議和它的存在,但如果我打電話checkIfSessionStillActive這沒有發現任何會議。 真的,我讀了很多代碼,我發現它幾乎是一樣的東西,可以幫助我嗎?

回答

0

你是否嘗試過使用setMaxInactiveInterval()而不是直接搞亂cookie?

+0

是的,同樣的事情,cookie說會話結束時的過期時間,但仍然返回NULL。 – gfelmer

1

您是否在使用擴展RemoteServiceServlet的子類並調用其他地方創建的對象(例如在Spring上下文中)並擴展了RemoteServiceServlet?如果是,則以下內容將解決您的問題

對於每個請求,都會創建一個新的RemoteServiceServlet實例。問題是在RemoteServiceServlet的超類中定義的線程局部變量不是靜態的,因此每個對象都有不同的變量。當你在上面的場景中處理調用時,你的請求響應線程局部變量被初始化爲接收到的對象,但是它沒有爲你調用方法的對象設置事物。

我在調用第二個對象之前,通過創建一個具有靜態threadlocal varrible和set值的Class的解決方法。現在,第二個對象也可以訪問它們。

+0

你可以多補充一點,你是怎麼做到的? – kozla13