2013-03-18 20 views
-1

打印實際值「request.getparameterValues」:不能在JSP

String[] UserList = ViewAllUserBasedOnRights(UserViewBy); 
request.getRequestDispatcher("/test/TestGetParameterValues.jsp?UserList="+UserList+"").forward(request, response); 

而JSP頁面顯示「[Ljava.lang。字符串; @ 8b3f2a「。

在這種方法「ViewAllUserBasedOnRights();」值從我的數據庫是收集和其將返回NULL或一組Strings

詳情:

我的JSP頁面,它很簡單的
public static String[] ViewAllUserBasedOnRights(String UserName) throws Exception{ 
       String[] retVal = null; 
       Connection con=null; 
       try{ 
       con= DatabaseConnectionManager.getDatabaseConnectionManager().getConnection(DatabaseConnectionManager.EAGRO_APP_DB); 
       con.setAutoCommit(false); 
       Statement stmt = (Statement) con.createStatement(); 
       Statement stmt1 = (Statement) con.createStatement(); 
       String SQLStringCount = "...."; 
       try(ResultSet rs = stmt.executeQuery(SQLStringCount)){ 
        int count = -1; 
        while(rs.next()){ 
         count = Integer.parseInt(rs.getString("COUNT")); 
        } 
        if(count > 0){ 
         String SQLString ="....."; 

        System.out.println("SQLString: "+SQLString); 
        try(ResultSet rs1 = stmt1.executeQuery(SQLString)){ 
         ArrayList<String> stringList = new ArrayList<String>(); 
         while(rs1.next()){ 
          stringList.add(rs1.getString("USER_NAME")); 
         } 
         String[] temp = new String[stringList.size()]; 
         retVal = (String[]) stringList.toArray(temp); 
         con.commit(); 
         rs1.close(); 

        }catch(SQLException e){ 
         try{ 
          if(con!=null) 
           extracted(con); 
          }catch(Exception ex){} 
         } 
       } 
      }catch(SQLException e){ 
       try{ 
        if(con!=null) 
         extracted(con); 
        }catch(Exception ex){} 
       } 

      }catch(SQLException e){ 
       try{ 
        if(con!=null) 
         extracted(con); 
        }catch(Exception ex){} 
       } 
      return retVal; 
     } 

代碼只是我趕上值和打印:

<% 
String[] UserList; 
UserList = (String[])request.getParameterValues("UserList"); 
for(int i = 0; i < UserList.length; i++){ 
%> 
<tr> 
<td style="border:1px solid black;" align="center"><%=i+1%> 
</td> 
<td style="border:1px solid black;" align="left" ><%=UserList[i]%> 
</td> 
</tr> 
<% } %> 
+0

建議接受有用的答案。 – 2013-04-05 07:41:56

回答

1

它不recommonded使用小腳本在jsp.You可以使用JSTL標記

<c:forEach items="UserList" var="temp"> 
    ${temp} 
</c:forEach> 

,並導入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

使用這個

0

從你的代碼之前,我看到你迭代ResultSet,然後創建String數組發送值時,創建ArrayList

由於您正在向JSP發送請求,因此您應該將ArrayList作爲請求對象發送給JSP,然後在此處進行迭代。

返回ArrayListreturn stringList;)從ViewAllUserBasedOnRights(UserViewBy)方法而不是String數組。

當轉發頁:

List<String> userList = ViewAllUserBasedOnRights(UserViewBy); 
request.setAttribute("userList", userList); 
request.getRequestDispatcher("/test/TestGetParameterValues.jsp").forward(request, response); 

JSP:

<% 
List<String> userList; 
int nSize=0; 
userList = (ArrayList<String>)request.getAttribute("userList"); 
if(userList!=null){ 
nSize= userList.size(); 
} 

for(int i = 0; i < nSize; i++){ 
%> 
<tr> 
<td style="border:1px solid black;" align="center"><%=i+1%> 
</td> 
<td style="border:1px solid black;" align="left" ><%=userList.get(i)%> 
</td> 
</tr> 
<% } %> 
0

感謝球員,我得到了解決,我已編輯在我的Servlet和JSP頁面下方

在Servlet的更改:

String[] UserList = ViewAllUserBasedOnRights(UserViewBy); 
request.setAttribute("userList", UserList); 
request.getRequestDispatcher("/test/TestGetParameterValues.jsp").forward(request, response); 

更改JSP頁面:

<% 
String[] UserList = null; 
if(request.getAttribute("UserList")!= null){ 
UserList = new String[((String[])request.getAttribute("UserList")).length]; 
UserList = ((String[])request.getAttribute("UserList")); 
} 
for(int i = 0; i < UserList.length; i++){ 
%> 
<tr> 
    <td style="border:1px solid black;" align="center"><%=i+1%> 
    </td> 
    <td style="border:1px solid black;" align="left" ><%=UserList[i]%> 
    </td> 
</tr> 
<% } %>