Iain Downie wrote:
Anyone out there know of an easy way to fill a Java ArrayList in a JSP from
an
array of strings held in a external JavaScript file? We need to load up a
large number of strings into the page and manipulate with JSP, and feel that
the best way is for the users to have the bulk of the text 'cached' on their
machine in a .js file rather than read from a database every time they
access. The strings will rarely change.
If not, any other better suggestions for cutting the speed of download?
Thanks
Iain
Of course this is possible. Given the understanding that a JSP generates HTML
dynamically, it isn't too much of a stretch to understand it can generate
Javascript dynamically as well. So your starting point is:
<script type="text/javascript"
src="somethingThatMakesAJavascriptArray.jsp"></script>
Then somethingThatMakesAJavascriptArray.jsp would read the database and generate
code that resembles:
var yourArray = [
"value1",
"value2",
"value3"
// etc
];
Doing this in server-side Javascript would be something like:
<%
// do the database retrieval into resultSet here complete
// with error checking; if resultSet is properly populated
// then proceed with the code below
Response.write('var yourArray = [\n');
var lastIndex = resultSet.rows - 1;
for (var i = 0; i < resultSet.rows; i++) {
resultSet.cursor = i;
Response.write('\t"' + resultSet.theColumn + '"');
if (i < lastIndex) {
Response.write(',');
}
}
Response.write('];');
%>
But depending on the amount of data involved, downloading this huge array to the
client might not be such a wise idea. Not to mention once the data is "cached"
in a client-side Javascript array, it can't be manipulated by JSP, it has to be
managed by client-side Javascript, something that might be problematic if you're
supporting multiple browsers.
--
| Grant Wagner <gw*****@agricoreunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
*
http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
*
http://www.mozilla.org/docs/web-deve...upgrade_2.html