当我通过Servlet通过以下代码调度JSP页面时:
String[] UserList = ViewAllUserBasedOnRights(UserViewBy);
request.getRequestDispatcher("/test/TestGetParameterValues.jsp?UserList="+UserList+"").forward(request, response);
JSP页面显示“ [Ljava.lang.String; @ 8b3f2a ”。
在此方法中,“ViewAllUserBasedOnRights();
”值将从我的数据库中收集,它将返回NULL
或一组Strings
。
详细说明:
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;
}
我的JSP页面的代码,它非常简单,我抓住了值并打印:
<%
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 :(得分:1)
不推荐在jsp中使用scriptlet。您可以使用jstl标签
<c:forEach items="UserList" var="temp">
${temp}
</c:forEach>
并导入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
使用之前
答案 1 :(得分:0)
从您的代码中,我看到您在迭代ArrayList
时创建ResultSet
,然后创建String数组以发送值。
因为,您要将请求发送到JSP,您应该将ArrayList
作为请求对象发送到JSP,然后在那里进行迭代。
从ArrayList
方法而不是return 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>
<% } %>
答案 2 :(得分: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>
<% } %>