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]

# 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.
# 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>