Results 1 to 2 of 2
  1. #1
    raabie is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default using javascript variable for jsp

    hello all, my code is like this:

    <%
    ArrayList<Item> itemList = new ArrayList<Item>();
    itemList = projr.getObjects(projectID);
    if (itemList.size() != 0) {
    %>

    <script type="text/javascript">
    window.onload = function() {

    for(var i = 0;i < <%=itemList.size()%>;i++){
    var azimuth=0;

    azimuth = <%=itemList.get(i).getAzimuth()%>;
    </script>

    <%
    }
    %>

    basically as you can see, due to certain reasons, i need to do the for loop within javascript. However, I cannot use the variable 'i' declared in javascript within the jsp<%=%> tag. Hence, I was wondering if there could be any work arounds.

    I've tried to store 'i' as a cookie and try to retrieve it in the jsp by doing smth like:
    azimuth = <%=itemList.get(Integer.parseInt((request.getCooki es())[0].getValue())).getAzimuth()%>;

    However, sadly this doesn't work. Also, I've thought of using hidden input fields to store 'i' but I don't think it would work as even if a did a request.getParameter(input name), i would not get anything as I have not submitted anything. Am I correct to say that?

    I would appreciate if any of you kind souls could help me out here =]

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You're making a common mistake here.

    The JSP code itself is run on the server. This creates the page you see on the client (ie browser), which is where the javascript will run. You cannot run Java code in you Javascript code (which seems to be what you;re attempting).

    You probably need to describe what it is you're trying to achieve.

Similar Threads

  1. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  2. how to access JSP variable in javascript
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-31-2010, 05:41 PM
  3. javascript variable
    By raghu9198 in forum New To Java
    Replies: 3
    Last Post: 01-29-2009, 08:42 AM
  4. javascript
    By fiero in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 02:45 PM
  5. Jsp and javascript
    By Ed in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 05:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •