Scope in JSP

Hi All,

I'm trying to build a data structure ArrayList [Hashtables] on the top of my jsp page (i.e. before the <html> tag> and iterate through that structure in my page <body>. However the ArrayList is not in scope. Can someone explain to me how I should declare and use the following code snippet:

<%

Hashtable ht =new Hashtable();

ht.put(Constants.COLUMN_A,"Column A Value");

ht.put(Constants.COLUMN_B,"Column B Value");

ht.put(Constants.COLUMN_C,"Column C Value");

ArrayList al =new ArrayList();

al.add(0,ht);

%>

<html>

...

<body>

<%

Hashtable tmp =new Hashtable();

for (int i = 0; i < a1.size();i++){

tmp = a1.get(i);

out.print(tmp.get(Constants.COLUMN_A));

out.print(tmp.get(Constants.COLUMN_B));

out.print(tmp.get(Constants.COLUMN_C));

}

%>

</body>

</html>

Question 1: Please explain how I should scope the above variables as ArrayList a1, is out of scope and the above code does not work as a result.

Question 2: Is there a way to clean up the rendering code using JSTL? Note that I am accessing my keys using a Java Constants file. Is there a way to do that using EL?

Thanks.

[1603 byte] By [joebassila] at [2007-11-27 9:54:00]
# 1
You do not have a scoping problem, you have a casting problem. When you call get(...) on an an ArrayList you get back an Object -- you need to cast it to the correct type you need, in this case, a Hashtable. Do you know how to do casting?
den2681a at 2007-7-13 0:23:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

My apologies, originally in my code within the body tag I was only doing this <%=a1.size();%>

I tried to expand my example to what I eventually wanted to do and forgot to cast the returned Object into a Hashtable. Can you provide feedback on the other aspects of the question(s)? Thanks!

joebassila at 2007-7-13 0:23:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I may not be to much help on your other request since it's hard to tell what your requirements are.

But basically what you want to be doing is loading all those Hashtables and ArrayLists inside some Action class (in java) and then passing them as form values to your jsp. You can then use tags to get the values and iterate over them, displaying them however you like.

I use Struts and <bean:write .../> tags for this sort of stuff. I'm not sure what framework you are using.

den2681a at 2007-7-13 0:23:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I understand that my code example is not very practical, but this was more or less an exercise for me to understand scoping in JSP. Assuming I created the ArrayList (as show in my code snippet), how would I access its element in the <body> tag was what I would like to find out. My requirements in this case are exactly as stated in my original post. Thanks.

joebassila at 2007-7-13 0:23:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

The code works fine for me. The only problem I found was some confusion in the variable naming 'a1' (a-one) is not the same as 'al' (a-el)

<%@ page import="java.util.*, java.lang.*" %>

<%

Hashtable ht = new Hashtable();

ht.put("COLUMN_A","Column A Value");

ht.put("COLUMN_B","Column B Value");

ht.put("COLUMN_C","Column C Value");

ArrayList a1 = new ArrayList();

a1.add(0,ht);

%>

<html>

...

<body>

<%

Hashtable tmp = new Hashtable();

for (int i = 0; i < a1.size();i++){

tmp = (Hashtable) a1.get(i);

out.print(tmp.get("COLUMN_A"));

out.print(tmp.get("COLUMN_B"));

out.print(tmp.get("COLUMN_C"));

}

%>

</body>

</html>

tolmanka at 2007-7-13 0:23:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I thought we already resolved your 'scoping' issue -- in that there was no issue and it was just a casting mistake. Do you still have a question about scoping?
den2681a at 2007-7-13 0:23:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
The issue was due to a typo, where I was using reference a1 in the body tag instead of al (a-el) as tolmank had pointed out in the previous post. Thanks for your help too!
joebassila at 2007-7-13 0:23:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...