Link to home
Start Free TrialLog in
Avatar of annie613
annie613

asked on

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??????
Avatar of annie613
annie613

ASKER

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????
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>
Sry. slight mistake in the last post. use this:
     <TD align="center"> <Font size="1">  <%=appList.get(applicationCtr+2)%>  </Font></TD>
<%=  appList.get(i+2)%>

variable i not found

not sure that will work?
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>
use the one in my last post and let me know what did it say?

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
ok...
to test, change
 for (int applicationCtr = 0; applicationCtr < appList.size(); applicationCtr+=2)

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

now try.

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

i should be getting R RW or N's

*stumped*
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>

ASKER CERTIFIED SOLUTION
Avatar of kupra1
kupra1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
figures the server is down now..i will have to try again later.. but i will keep you posted! :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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*
JSTL is all in the JSP. It's the standard taglibs and makes things more like HTML type tags rather then scriptlets
and this can be done dynamically as well???

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??????
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.
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 :)
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>
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???
do i need another loop for the privCtr???
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
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".
you should increase your "privCtr" accordingly to the place where your pri value store.....example like privCtr=privCtr+3....
hope this can help you...:)
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