2012-02-02 28 views
0

我正在處理一個嚴重問題,似乎找不到合理的解決方案。 它在這裏。 我在我的代碼(jsp文件)中有一個字符串數組。我想在同一個頁面中傳遞數組,並且我想讓數組成爲一個會話並稍後在代碼中再次調用它,但似乎我無法將會話(使用get.Attribute)並再次創建爲數組。爲了更具體說明,以下代碼可能會對您有所幫助。字符串數組和會話

 while (onomaq.next()) { 
            String onomatemp = onomaq.getString("one1"); 
            String[] onoma = onomatemp.split(" "); 
            out.println(onoma[2]); 
            session.setAttribute("onoma", onoma); 
           } 
          } catch (Exception e) { 
           System.out.println("SQL Exception: " + e.toString()); 
          } 

         %> 

         <% 
try{ 
    Object o = session.getAttribute("onoma"); 
String k=o.toString(); 
String[] name=k.split(","); 
out.println(name[1]); 

} 
catch (Exception e) 
     { 
                  System.out.println("SQL Exception: " + e.toString()); 
          } 

out.println給我留言lige [L.java.String和一些字符。

任何人都可以幫助我嗎?

+0

修復您的文章的一些可讀代碼... – L7ColWinters 2012-02-02 17:23:26

回答

3

而不是在從會話中獲取數組對象之後調用toString(),只需將對象引用轉換爲數組(因爲您的對象是數組)並使用它。與

String[] name= (String[]) session.getAttribute("onoma"); 

P.S.

Object o = session.getAttribute("onoma"); 
String k=o.toString(); 
String[] name=k.split(","); 

這意味着,代替這個代碼toString()的目的與你期望的目的有點不同。請參閱Javadoc