File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes using and passing a multidimensional array to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "using and passing a multidimensional array to jsp" Watch "using and passing a multidimensional array to jsp" New topic

using and passing a multidimensional array to jsp

Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
I have an application where I have a servlet connect to a db and perform sql commands, put the sql results in a multidimensional array, and then pass the multidimensional array to a jsp page to display. However, when I try to do this, something is going wrong, but I'm not sure what it is because I'm not getting any kind of error message. I have pinpointed to where the error is occuring and its when I try to set the values of the array, at this line:
resultsArray[v][s] = rs2.getString(s+1);
Here is the servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class ProjectDetailProcess3 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
ConnectionPool connPool = null;
public void init(ServletConfig config) throws ServletException {
String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc dbc:ProjectsDb";
try {
connPool = new ConnectionPool(jdbcDriver, dbURL, "projects", "projects");
}catch (Exception e) {
System.out.println("Error " +e);

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String securityAccess = request.getParameter("pmsn");
int projectId = Integer.parseInt(request.getParameter("project"));
int projectDetailId = Integer.parseInt(request.getParameter("projDetail"));
Connection dbConn = null;
dbConn = connPool.getConnection();
Statement stmt = dbConn.createStatement();
int numberOfRows = 0;
String getRowCount = "Select count(*) from projects";
ResultSet rs = stmt.executeQuery(getRowCount);
while ( {
numberOfRows = Integer.parseInt(rs.getString(1));
String getDetails1 = "Select * from projects_detail";
ResultSet rs2 = stmt.executeQuery(getDetails1);
int numberOfColumns = rs2.getMetaData().getColumnCount();
String[][] resultsArray = new String[numberOfRows][numberOfColumns];
while ( {
for (int v=0; v<=numberOfRows-1;v++) {
for (int s=0;s<=numberOfColumns-1;s++) {
resultsArray[v][s] = rs2.getString(s+1);
HttpSession session = request.getSession();
request.setAttribute("array", resultsArray);
RequestDispatcher d = request.getRequestDispatcher("/jsp/Test.jsp");
d.include(request, response);
}catch (ServletException sx) {
}catch (Exception e) {
public void destroy() {
Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
I think the problem is comng from the while loop, when I'm looping through the resultset. What I am trying to do is capture all of the data in a 2 dimension array. One dimension the rows the other dimension the columns. When I comment out the line of code where the array is getting assinged, it works. But I have tried to redo the while statement, but still I am not able to get it to work. Does any one have any ideas?
Thanks in advance.
Manjunath Reddy
Ranch Hand

Joined: Jul 26, 2001
Posts: 60
The logic seems alright to me...just check if the no of rows returned by query "select count(*) from projects" matches with the query "select * from project_details". It seems that u r assuming that the rows of the project table matches with that of the project_details...but they might not be so..
I agree. Here's the link:
subject: using and passing a multidimensional array to jsp
It's not a secret anymore!