• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving values

 
Vijendra Runwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to pass String Array from jsp page to java Script. I am passing the string array from action class to form. How to get the values of string array on jsp page and from there to java script.


[ May 18, 2006: Message edited by: Bear Bibeault ]
 
Sri Gnana
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using dropdown menu you can store the value.
In javascript use select object.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not 'pass' anything from a JSP page to a browser.
JSP pages build text and stream them to the browser.
The browser interprets the text as HTML, Javascript, etc..

To recreate a server side Java array within a client side Javascript environment, you will need to write out the array in the page you're building.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code

<%
String[] s = {"one","two","three"};

for(int i = 0; i<s.length(); i++)
{
%>
<SCRIPT>
selectarray[count] = '<%= s[count]>';
count++;
</SCRIPT>

<%
}
%>


count and selectarray are javascript variables
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the 'count' variable? It would also be better to use JSTL rather than scriptlets.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
count variable is for indexing for the selectarray and string array s
to increment for each loop

Yes, using JSTL is good programing comparing with script
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by naveen gupta:
count variable is for indexing for the selectarray


Unecessary. Just use the array.push() method to add the entry to the end of the array, or use the scripting variable i for direct assignment.

Or better yet, use array notation and forget about indexes completely.
[ May 18, 2006: Message edited by: Bear Bibeault ]
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variable [b]i/[b] is declared inside scriptlet code, so i think you can't access that variable inside javascript
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by naveen gupta:
variable i is declared inside scriptlet code, so i think you can't access that variable inside javascript


Javascript in the browser and any type of JSP/Java variable on the server exist in two different worlds. One can never access the the other.
All you can do is use the mechanisms provided by JSP to write them to the page as Javascript literals.

A useful exercise for understanding this is to view the HTML source of the page from your browser. This is what the JSP code creates.
Another useful excersise is to write the entire page as static HTML and get it to run in a browser. Then, once the Javascript is working with mock data in the array, figure out how you want to build the same thing using JSP and live data.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by naveen gupta:
variable [b]i/[b] is declared inside scriptlet code, so i think you can't access that variable inside javascript


No, but in the same way that you are wirintg out the string array values, you can write out the value of i.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic