Forum Stats

  • 3,783,474 Users
  • 2,254,783 Discussions
  • 7,880,426 Comments

Discussions

Javascript Problem

Prem
Prem Member Posts: 88
Hi I am facing one issue

I have one jsp file which is having following javascript Fucntion

function myFunction(obj){
if(obj!=null){
<%
List<String> arrayList= new ArrayList<String>();
%>
var entry = obj.id
<% arrayList.add(%>entry<%); %>
<% request.setAttribute("arrayListAttribute",arrayList); %>
}
}

Above JS function is fired when some onlick event is get fired on some elements say.

Now Problem is I am not able to add that element's ID into arraylist . This jsp is not compiling due to this.

Please help me out.
Tagged:

Answers

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Jan 20, 2012 9:37AM
    I'll give a big hint: Java (JSP) executes on the server, Javascript is executed by the browser (the client). Do you still need to be told why you are horrible mistaken?
  • Prem
    Prem Member Posts: 88
    Hi ,

    Yes I am aware about this , but questioned here so that some one can come up with alternative.

    Do you have any alternative on this ?
  • ErasP
    ErasP Member Posts: 7
    Hi.

    I have done something similar in a project using ajax and php, but the basics should be the same.
    var request = new XMLHttpRequest();
    
    function myFunction(obj){
    if(obj!=null){
    var entry = obj.id;
    
    request.open("GET", "http://yoururl/file.jsp?obj="+entry, true);
    
    request.sent(null);
    }
    }
    Then in your jsp you can add the object into the arrayList.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    Prem wrote:
    Yes I am aware about this , but questioned here so that some one can come up with alternative.
    You were not aware, or otherwise you would not have even gotten as far as writing the above piece of code. Nowhere did you ask for an alternative either, unless you were expecting people to read your mind.

    Ajax is one such alternative as the previous poster, simply resubmitting the page so it can rerender is another if ajax is beyond your skill level right now.
This discussion has been closed.