displaying data to webpage

i never worked with array lists before... i have a question about display data to a table

here i have a .jsp file

.
.
.

<%

.
.
.

  int privCtr = 0;
  ArrayList appList = um.getApplications();
  ArrayList reportList = um.getUserPrivilgeReport(employee);
  ArrayList activityList = access.getActivities();
 
%>

<html>

  <head>
  <Title>test page</Title>
  </head>
 
  <body>


    <Center><H4>test page for now</H4></Center>
    <Table border="1" width="100%" cellspacing="0" cellpadding="1">
      <TR>
        <TH>&nbsp;</TH>
      <%

out.println("reportList: " + reportList);//this is the dynamic content -- it prints out an ArrayList -- ao, app_id, priv --   reportList: [T6, 1, R, T6, 2, R, T6, 4, R, T6, 5, R, ...and so on]
//R is the element i would like to display in my table its the priv (can be R RW or N -- depends on employee)


      for (int i = 1; i < appList.size(); i+= 2)
      {%>
        <TH><font size="1"> <%= appList.get(i) %> </font></TH>
      <%}%>
      </TR>
      <%
      for (int rowCtr = 0; rowCtr < activityList.size(); rowCtr++)
      {
        Activity activity = (Activity)activityList.get(rowCtr);
      %>
      <TR>
      <TD nowrap><font size="1"> <%= activity.getAcronym()+"-"+activity.getActivity() %> </font></TD>
      <%
        for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+=2)
      {%>
        <TD align="center"> <Font size="1">  R  </Font></TD>
      <%}%>
      </TR>
     <%}%>  

    </Table>
   
  </body>
 
</html>


this line of code
<TD align="center"> <Font size="1">  R  </Font></TD>
prints R to the screen however i would like to have this set dynamically if the user has R, RW or N as the priv
i thought doing this would work
<TD align="center"> <Font size="1">  <%= applicationCtr %>  </Font></TD>
it prints the app_id but not the dynamic part of the data i need

my java looks like this...

public ArrayList getUserPrivilgeReport(String employee)
  {
    ArrayList reportList = new ArrayList();
    String priv = "";

    try
    {
      makeConnection();
      ps = conn.prepareStatement( queryGetUserPrivilgeReport);//query works fine
      ps.setString(1, employee);
      rs = (ResultSet)ps.executeQuery();
     
      while (rs.next())
      {
        reportList.add(rs.getString("ao"));
        reportList.add(rs.getString("app_id"));

        if ( rs.getInt("priv") == 1 )
          priv = "R";
         
        else if ( rs.getInt("priv") == 2 )
          priv = "RW";
         
        else
          priv = "N";
          reportList.add(priv);
      }
    }
    catch(Exception e)
    {
      System.out.println("User Manager exception for getUserPrivilgeReport(" + employee + "): " + e.toString());
    }
    finally
    {
      try
      {
        releaseConnection();
      }
      catch(Exception e){}
    }

    return reportList;

  }//getUserPrivilgeReport

is there an easy way to work with ArrayLists just to pass the one element to the table on the jsp page??????
annie613Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

annie613Author Commented:
im trying to work a loop structure in the java file but im having some trouble
i thought maybe in the while having a String for the reportList as an array

like

String rl[] = new String[3];
rl[0] = ao;
rl[1] = app_id;
rl[2] = priv;

reportList.add(rl);

am i on the right track????
kupra1Commented:
try this.
replace this:
        <TD align="center"> <Font size="1">  <%= applicationCtr %>  </Font></TD>
by
        <TD align="center"> <Font size="1">  <%=  appList.get(i+2)%>  </Font></TD>
kupra1Commented:
Sry. slight mistake in the last post. use this:
     <TD align="center"> <Font size="1">  <%=appList.get(applicationCtr+2)%>  </Font></TD>
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

annie613Author Commented:
<%=  appList.get(i+2)%>

variable i not found

not sure that will work?
annie613Author Commented:
i get this error

The Error that occured was : java.lang.IndexOutOfBoundsException: Index: 44, Size: 44

with this line of code
<TD align="center"> <Font size="1">  <%=appList.get(applicationCtr+2)%>  </Font></TD>
kupra1Commented:
use the one in my last post and let me know what did it say?
annie613Author Commented:

here are the results for both suggestions :)

<TD align="center"> <Font size="1"> <%=  appList.get(i+2)%>    </Font></TD>
Error(65,75): variable i not found in class _userReport


<TD align="center"> <Font size="1"> <%=appList.get(applicationCtr+2)%>   </Font></TD>
The Error that occured was : java.lang.IndexOutOfBoundsException: Index: 44, Size: 44
kupra1Commented:
ok...
to test, change
 for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+=2)

to
 for (int applicationCtr = 0; applicationCtr < 2; applicationCtr+=2)

now try.

annie613Author Commented:
it runs but all i get is the first column filled with 1's

i should be getting R RW or N's

*stumped*
kupra1Commented:
ohh.. my mistake. The ArrayList starts from the index 1.
So, use this:
<TD align="center"> <Font size="1"> <%=appList.get(applicationCtr+3)%>   </Font></TD>

kupra1Commented:
use this.
    <%
        for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+=3)
      {%>
        <TD align="center"> <Font size="1"> <%=appList.get(applicationCtr+3)%>   </Font></TD>
      <%}%>
      </TR>
     <%}%>  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
annie613Author Commented:
figures the server is down now..i will have to try again later.. but i will keep you posted! :)
tang_tzuchiGeneral ManagerCommented:

i think tihis can solve your problem....
hopes can help you...:)

<%for(int applicationCtr=2;applicationCtr<appList.size();applicationCtr+=3){
%>

<TD align="center"><Font size="1"> <%appList.get(applicationCtr)%></Font></TD>
<%}%>

</TR>
<%}%>
bloodredsunCommented:
If you are looking to iterate over arraylists in JSPs you may want to consider using JSTL to do this as it is very easy.

here's an example of how to do it
----------------------------
<%@ page import="java.util.*, com.bloodredsun.dev.Person" %>
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<html>
<head><title>JSTL c:forEach example</title></head>
<body>
<br />

<%
ArrayList arrList = new ArrayList() ; //an example using a java class
arrList.add( new Person("friend" , 30) ) ;
arrList.add( new Person("enemy" , 34) ) ;
arrList.add( new Person("mother" , 50) ) ;
arrList.add( new Person("father" , 55) ) ;

ArrayList arrList2 = new ArrayList() ; //an example using a String
arrList2.add( "Bert" ) ;
arrList2.add( "Ernie" ) ;
arrList2.add( "Bob" ) ;
arrList2.add( "Jeff" ) ;

request.setAttribute("myList", arrList );
request.setAttribute("myList2", arrList2 );

%>

<c:forEach var='alItem' items='${requestScope.myList}' varStatus='status'>
      Status:<c:out value='${alItem.status}' />
      Age:<c:out value='${alItem.age}' />
      Count: <c:out value='${status.count}'/>
      <br />
            <c:if test='${status.count == 1}'>
            <c:forEach var='item' items='${requestScope.myList2}'>
            <font color="red">Names: <c:out value='${item}'/></font>
            <br />
            </c:forEach>
      </c:if>
</c:forEach>
annie613Author Commented:
kupra1 - still no luck with this code
for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+=3)
      {%>
        <TD align="center"> <Font size="1"> <%=appList.get(applicationCtr+3)%>   </Font></TD>

it returns only the first column with the report name


tang the code you posted inserts the reports and the appids in the table and not the priv

bloodredsun -- i think what you are doing in your post is what i need, however i am not familiar with JSTL
this is all added to the jsp file or do i need to put some of this in the java file
and call it in the jsp file???

im still going to play around with this.. any other suggestions are welcome! cheers annie
annie613Author Commented:
if i use this code
<TR>
      <TD nowrap><font size="1"> <%= activity.getAcronym()+"-"+activity.getActivity() %> </font></TD>
      <%
        for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+= 2 )
        //for ( int applicationCtr = 2; applicationCtr < appList.size(); applicationCtr+= 3)
      {%>
        <TD align="center"> <Font size="1"> <%= appList.get(applicationCtr) %>   </Font></TD>
      <%}%>
      </TR>

i get the appids in the correct places. why is it so hard to get the priv (R RW N) *still stumped*
bloodredsunCommented:
JSTL is all in the JSP. It's the standard taglibs and makes things more like HTML type tags rather then scriptlets
annie613Author Commented:
and this can be done dynamically as well???
annie613Author Commented:

so let me see if i understand this -- i need one ArrayList as a String
such as your example

ArrayList arrList2 = new ArrayList() ; //an example using a String
arrList2.add( "Bert" ) ;
arrList2.add( "Ernie" ) ;
arrList2.add( "Bob" ) ;
arrList2.add( "Jeff" ) ;

mine like this

ArrayList reportList = new ArrayList();

reportList.add(rs.getString("ao"));
reportList.add(rs.getString("app_id"));

if ( rs.getInt("priv") == 1 )
priv = "R";
         
else if ( rs.getInt("priv") == 2 )
priv = "RW";
         
else
priv = "N";
reportList.add(priv);

request.setAttribute("myReportList", reportList);

and then the rest of the code altered??????
bloodredsunCommented:
Yep. JSTL is not the best for creating arraylists and the like as that is not what it is designed for. Hopefully you would be creating the objects somewhere else and just using the JSP as the presentation layer (I don't know whether you are familiar with MVC architecture)

What it is designed for is to simplify the process of displaying dynamic data, either from a java class, xml file or database with easy formating is required.
annie613Author Commented:
its is designed to process dynamic data based on a employee id from the java class and display it in a table in the jsp
but im still not understanding the whole process

i thought i would be able to just set the priv in the java file and display it in the jsp
but it is not that simple :(

but everyone's comments are helping me out more and more :)
annie613Author Commented:
is there anyway to do this without an ArrayList????

<Table border="1" width="100%" cellspacing="0" cellpadding="1">
      <TR>
        <TH>&nbsp;</TH>
      <%
     
out.println("reportList: " + reportList);//this is the report -- prints out --   reportList: [T6, 0, R, T6, 1, R, T6, 2, R, T6, 5, RW,  ... and so on ... ]
//only need the privs to display in table (R, RW, N) -- third element in each set of three


      for (int i = 1; i < appList.size(); i+= 2)
      {%>
        <TH><font size="1"> <%= appList.get(i) %> </font></TH>
      <%}%>
      </TR>
     
      <%
      for (int rowCtr = 0; rowCtr < activityList.size(); rowCtr++)
      {
        Activity activity = (Activity)activityList.get(rowCtr);
      %>
     
      <TR>
      <TD nowrap><font size="1"> <%= activity.getAcronym()+"-"+activity.getActivity() %> </font></TD>
      <%
          for (int applicationCtr = 0; applicationCtr < reportList.size(); applicationCtr+= 2 )
      {%>  
         <TD align="center"> <Font size="1"> <%= reportList.get(applicationCtr) %>   </Font></TD> //this is my trouble spot Still
      <%}%>
      </TR>
     <%}%>  
    </Table>
annie613Author Commented:
i think im getting closer

<TR>
      <TD nowrap><font size="1"> <%= activity.getAcronym()+"-"+activity.getActivity() %> </font></TD>
      <%
        for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+= 2 )
        {%>  
         <TD align="center"> <Font size="1">  <%= reportList.get( privCtr + 2 ) %>   </Font></TD>
        <%}%>
      </TR>

this returns the privs but they are all R's

any suggestions???? why they are not dynamic???
annie613Author Commented:
do i need another loop for the privCtr???
annie613Author Commented:
new idea -- back to the arrayList idea :)

// .java file
public ArrayList getUserPrivilgeReport(String employee) //throws Exception
  {
    String methodName = "sf.getUserPrivilgeReport";
    String paramList = employee;

    ArrayList reportList = new ArrayList();
    String prevActivity = "";
    boolean firstTime = true;

    try
    {
      makeConnection();
      ps = conn.prepareStatement( queryGetUserPrivilgeReport );
      ps.setString(1, employee);
      rs = (ResultSet)ps.executeQuery();
           
      String activity = "";
      String acronym = "";
      String priv = "";
     
      while (rs.next())
      {
        activity = rs.getString("ACTIVITYCODE");
       
         if ((!activity.equals(prevActivity)) && (firstTime == false))
        {
          String actpriv[] = new String[3];
          actpriv[0] = prevActivity;
          actpriv[1] = acronym;
          actpriv[2] = priv;
         
          reportList.add(actpriv);
         
          priv = rs.getString("PRIV");

          acronym = rs.getString("ACRONYM");
          prevActivity = activity;
        } //if

        else
        {
          if (firstTime == true)
          {
            priv = rs.getString("PRIV");
   
            acronym = rs.getString("ACRONYM");
            firstTime = false;
          }
          else
          {
            priv += "," + rs.getString("PRIV");
            acronym = rs.getString("ACRONYM");
          }

          prevActivity = activity;
        } //else
       
      } //while

      String actpriv[] = new String[3];
      actpriv[0] = prevActivity;
      actpriv[1] = acronym;
      actpriv[2] = priv;
         
         
      reportList.add(actpriv);
     
      rs.close();
    } //try    
    catch (Exception e)
    {
      // Write an error message to the web log.
      //logError(methodName, paramList, e, null);
      System.out.println("User Manager exception for getUserPrivilgeReport(" + employee + "): " + e.toString());
    }
    finally
    {
      try
      {
        releaseConnection();
      }
      catch (Exception e)
      {
        // Write an error message to the web log.
        //logError(methodName, paramList, e, null);
        System.out.println("User Manager exception for getUserPrivilgeReport(" + employee + "): " + e.toString());
      }
    }
    System.out.println(reportList);
    return reportList;
  } //getUserPrivilgeReport

//.jsp file

will get back to this file shortly
tang_tzuchiGeneral ManagerCommented:
i think im getting closer

<TR>
      <TD nowrap><font size="1"> <%= activity.getAcronym()+"-"+activity.getActivity() %> </font></TD>
      <%
        for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+= 2 )
        {%>  
         <TD align="center"> <Font size="1">  <%= reportList.get( privCtr + 2 ) %>   </Font></TD>
        <%}%>
      </TR>

this returns the privs but they are all R's

any suggestions???? why they are not dynamic???


for the solution you use on top,i think the probelm t5hat you cannot display the data dynamically is because your variable "privCtr" did not change accordingly so it will remain the first value that is the "R".
tang_tzuchiGeneral ManagerCommented:
you should increase your "privCtr" accordingly to the place where your pri value store.....example like privCtr=privCtr+3....
hope this can help you...:)
annie613Author Commented:
for the most part i just edited my java file and did this to my jsp

<Table border="1" width="100%" cellspacing="0" cellpadding="2">
       
     <TR>
        <TH><font size="3"> Report </font> </TH>
        <TH><font size="3"> Read Privs </font> </TH>
        <TH><font size="3"> Write Privs </font> </TH>
       
     </TR>
     
      <% for( int i = 0; i < reportList.size(); i ++)
      {
        String[] privs = (String[])reportList.get(i);%>
 
      <TR>        
        <TD align="center"> <font size="3"> <%=privs[0] %> </font> </TD>
        <TD align="left"> <font size="2"> <%=privs[1] %> </font> </TD>
        <% if(privs[2].equals(""))
        {%>
          <TD align="left"> <font size="2"> &nbsp; </font> </TD>
        <%}
        else
        {%>
          <TD align="left"> <font size="2"> <%=privs[2] %> </font> </TD>
        <%}%>
       
      </TR>
      <%}%>
    </Table>

in the java file method i call from the jsp

i have an ArrayList and use a String[] then add the String[] to the ArrayList

it works
cheers to all who gave great input!!! :)

i divided the points by 3 since everyone helped out and helped me arrive at the solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.