aspose file tools*
The moose likes JSP and the fly likes Retrieving values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Retrieving values" Watch "Retrieving values" New topic
Author

Retrieving values

Vijendra Runwal
Ranch Hand

Joined: Apr 17, 2006
Posts: 52
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 ]

SCJP 1.4,SCWCD
Sri Gnana
Ranch Hand

Joined: Apr 29, 2004
Posts: 166
By using dropdown menu you can store the value.
In javascript use select object.


Thanks & Regards
Sri Gnana
Everythings Programmed!...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
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

Joined: Jan 10, 2002
Posts: 60997
    
  65

Why the 'count' variable? It would also be better to use JSTL rather than scriptlets.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
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

Joined: Jan 10, 2002
Posts: 60997
    
  65

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

Joined: Apr 12, 2006
Posts: 129
variable [b]i/[b] is declared inside scriptlet code, so i think you can't access that variable inside javascript
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 10, 2002
Posts: 60997
    
  65

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

Joined: Apr 12, 2006
Posts: 129
got it
 
 
subject: Retrieving values