LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-20-2008, 12:55 AM   #1
sajith
Member
 
Registered: Sep 2005
Location: kannur
Posts: 59

Rep: Reputation: 15
arraylist in javascript


sir,

i have a arraylist in jsp, i need to pass this arraylist to javascript and store there.

For some validations i need to refer this stored arraylist in javascript.
And display some messages to user.

how can i pass arraylist object fron jsp to javscript.
how can i search this arraylist in javascript for some specified value.

waiting for your reply......
 
Old 03-22-2008, 12:04 PM   #2
Jesdisciple
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Rep: Reputation: 0
I don't know JSP, but I do know how to inject server variables into JavaScript. In PHP it would be something like this:
Code:
//JavaScript document
<?php
$arr = array('Bob', 'Tom', 'Harry');
?>
var arr = <?php echo('[' . implode(',', "'" . $arr . "'") . ']'); ?>;
The function 'echo' writes output to the client; 'implode' puts each array value, surrounded by single-quotes, together with a comma between each consecutive pair. The dots concatenate strings together like a plus-sign does in Java. The output of the above would be:
Code:
//JavaScript document

var arr = ['Bob','Tom','Harry'];

Last edited by Jesdisciple; 03-22-2008 at 12:14 PM.
 
Old 03-23-2008, 05:44 PM   #3
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
You cannot pass the arraylist object from JSP to JS directly. You need to loop through each value and construct the JS array object.

Here is the sample code snippets.

<script language="javascript">
var tempArr = new Array();
<%
for (int i=0; i<tempArrayList.size(); i++){%>
tempArr[<%=i%>] = <%=(String)tempArrayList.get(i);%>
<%
}
%>
</script>

Fedora Development

Last edited by prad77; 04-17-2008 at 03:23 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort test for an ArrayList (java) thefalling Programming 3 12-02-2006 10:32 PM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
ArrayList question (Java) ksgill Programming 5 10-14-2003 02:00 PM
Java ArrayList question ksgill Programming 2 10-12-2003 07:54 AM
Help with javascript? BajaNick Linux - Software 2 07-20-2003 08:41 PM


All times are GMT -5. The time now is 09:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration