2009-10-15 65 views
0

我在處理會話中的對象時遇到問題。在Struts應用程序中處理會話

我存儲的對象而在這樣的會議。假設對象是對象的名稱。我做這在我的動作類:

if(object!=null) 
{ 
session.settAttribute("objectName",object); 
return mapping.findForward("success"); 
} 
else 
{ 
return mapping.findForward("failure"); 
} 

我映射成功和失敗的相同的JSP頁面。我檢查就像

if(session.getAttribute("objectName")!=null) 
    { 
     object= (SomeObjectClass)session.getAttribute("objectName"); 
    } 
    if(object!=null) 
    { 
    //Do this 
    } 
    else 
    { 
    //Do that 
    } 

現在來了我的問題。在會話中第一次設置對象時沒有問題。當我從兩個不同的瀏覽器同時調用這個操作類時,我遇到了一個問題,同時我去其他部分處理一個案例,如果部分處理一個案例。我相信這是因爲會話不是線程安全的。有沒有解決方法?

回答

0

當您訪問從不同的瀏覽器操作/頁面創建一個新的會話。在現代瀏覽器中,您可以在選項卡或視圖之間共享會話。與更多瀏覽器共享會話的唯一方法是在URL中使用jSessionid參數。

+0

在動作類檢索 - 絕對正確的,但不是在所有樂於助人:-) – ChssPly76 2009-10-15 06:09:04

+0

我會糾正自己在一分鐘內:) – cetnar 2009-10-15 06:10:57

+0

我發現什麼是problem.its因爲我已經宣佈 的HttpSession之外,這使得全球會議每一個線程調用,因此疑難問題的excecute方法已糾正它。 – Harish 2009-10-15 07:47:42

1

你提到你想看到兩個瀏覽器之間的相同的信息......如果你想分享的信息是「全球性」(即它應該是應用程序的所有用戶一樣,你應該存儲在應用範圍,而不是會話範圍的信息(見http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html#JSPIntro5作用域的解釋),例如:。

ServletContext servletContext = getServlet().getServletContext(); //"application scope" 
SomeObjectClass object = (SomeObjectClass) servletContext.getAttribute("objectName"); 

if(object !=null){ 
    //Do this 
} else { 
    //Do that 
} 

如果你有賬戶和登錄機制,你在想同一登錄在兩個不同的瀏覽器中看到相同的信息,那麼你有一個不同的問題,在這種情況下,信息需要存儲在一個「數據庫」中(不一定是rdbms,可能是應用程序範圍! EDS的持久性)和信息需要使用口語,像一個真正的程序員可能被存儲在會話,cookie中的用戶信息等

//get the user info from the session, cookies, whatever 
UserInfoObject userInfo = getUserInfo(request); 
//get the object from the data store using the user info 
SomeObjectClass object = getObjectForUser(userinfo); 

if(object !=null){ 
    //Do this 
} else { 
    //Do that 
}