4 Replies Latest reply: Jan 23, 2012 2:55 AM by gimbal2 RSS

    Javascript Problem

    Prem
      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.
        • 1. Re: Javascript Problem
          gimbal2
          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?
          • 2. Re: Javascript Problem
            Prem
            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 ?
            • 3. Re: Javascript Problem
              ErasP
              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.
              • 4. Re: Javascript Problem
                gimbal2
                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.