[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

A JSP Question


I am trying to assign the String values from an array in Java to a javascript array. I am assuming that the LOOP tag in JSP can help me but I am not sure and don't know how to use it. could you give some concrete example(s). I don't have to use LOOP. Whatever works is fine with me. I would really appreciate.


1 Solution
LOOP is a non-standard JSP tag supported by some products like JRUN
If you want to develop standard code, you cant' use LOOP.
kmahmoodAuthor Commented:
Edited text of question.
you could write standard java code (in a .jsp file) to loop thru an array and you could then assign these to javascript variables by using the
<%= variable %> type tags of JSP.
smthin like:

 for(int i=0; i < numOfElements; i++)
//ur JavaScript to assign values
var myvar = <%= urArray(i) %> ;
i think this should work,
bye - Apache.
If all you want to do is get the array out then Values passed from Java to JavaScript are converted as follows:

 (note the point about strings at the end...)

Java byte, char, short, int, long, float, and double are converted to JavaScript numbers.

A Java boolean is converted to a JavaScript boolean.

An object of class netscape.javascript.JSObject is converted to the original JavaScript object.

Java arrays are converted to a JavaScript pseudo-Array object; this object behaves just like a JavaScript Array object: you can access it with the syntax arrayName[index] (where index is an integer), and determine its length with arrayName.length.

A Java object of any other class is converted to a JavaScript wrapper, which can be used to access methods and fields of the Java object:

Converting this wrapper to a string calls the toString method on the original object.

Converting to a number calls the doubleValue method, if possible, and fails otherwise.

Converting to a boolean in JavaScript 1.3 returns false if the object is null, and true otherwise.

Converting to a boolean in JavaScript 1.2 and earlier versions calls the booleanValue method, if possible, and fails otherwise.
Note that instances of java.lang.Double and java.lang.Integer are converted to JavaScript objects, not to JavaScript numbers. Similarly, instances of java.lang.String are also converted to JavaScript objects, not to JavaScript strings.

Java String objects also correspond to JavaScript wrappers. If you call a JavaScript method that requires a JavaScript string and pass it this wrapper, you'll get an error. Instead, convert the wrapper to a JavaScript string by appending the empty string to it, as shown here:

var JavaString = JavaObj.methodThatReturnsAString();
var JavaScriptString = JavaString + "";


Finally, to get at the applet from javascript use:

Referring to Applets

Each applet in a document is reflected in JavaScript as document.appletName, where appletName is the value of the NAME attribute of the <APPLET> tag. The applets array also contains all the applets in a page; you can refer to elements of the array through the applet name (as in an associative array) or by the ordinal number of the applet on the page (starting from zero).

For example, consider the basic "Hello World" applet in Java:

import java.applet.Applet;import java.awt.Graphics;
public class HelloWorld extends Applet {
   public void paint(Graphics g) {
      g.drawString("Hello world!", 50, 25);

The following HTML runs and displays the applet, and names it "HelloWorld" (with the NAME attribute):

<APPLET CODE="HelloWorld.class" NAME="HelloWorld" WIDTH=150 HEIGHT=25></APPLET>

If this is the first applet in the document (topmost on the page), you can refer to it in JavaScript in any of the following ways:


The applets array has a length property, document.applets.length, that indicates the number of applets in the document.

All public variables declared in an applet, and its ancestor classes and packages are available in JavaScript. Static methods and properties declared in an applet are available to JavaScript as methods and properties of the Applet object. You can get and set property values, and you can call methods that return string, numeric, and boolean values.

Everything else you need to know is here:


Go to Chapter 15, Liveconnect Overview for the above details and more...

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now