0

There is a JSP page in which i have an ArrayList variable. I need these arraylist values(txt1 and txt2) in script tag.I have tried lots of combinations and googling but didnot succeed.

javascript function

function fun1()
{   
   // how to get arraylist values      
}

JSP page:

 <body>
       <%
           ArrayList<String> al= new ArrayList<String>();
           al.add("txt1");
           al.add("txt2");

           out.println("<input type=text id=" + al.get(0) + ">");out.println("<br>");
           out.println("<input type=text id= " + al.get(1) + ">");out.println("<br>");
           out.println("<input type=button id=btn1  value=click onClick=fun1(); > ");
       %>
 </body>

Thanks for reply

4 Answers 4

1

Inside javascript you can use Scriplets very well

Assume the list 'arrayList' is having [1,2,3,4] in java (JSP)

you can get the array list by this

function fun1()
{   
   var list = '<%= arrayList %>';
}

if you print the variable list, it will be a string with value '[1,2,3,4]'

You can then split this using Regex or simple string operation

<body>
       <%
           ArrayList<String> al= new ArrayList<String>();
           al.add("txt1");
           al.add("txt2");

           out.println("<input type=text id=" + al.get(0) + ">");out.println("<br>");
           out.println("<input type=text id= " + al.get(1) + ">");out.println("<br>");
           out.println("<input type=button id=btn1  value=click onClick=fun1(); > ");
       %>
 </body>

<script>
 // try the script here
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

I tried var list='<%= al %>' , but it gives error saying " al can not be resolved"
Your jsp code must be above the javascript for the scope to work. Put your JSP Code first and then the javscript code.
Because scope of array list is local. use scriptlets <%! declarations %> instead of <% declarations %>.
Dude.. he he.. there is an option called Accept . Please click that. So that , people who see the same problem in future, can direct take the answer instead trying out all the solution :)
0

Try something like this.

<script>
var myArray=new Array();
myArray[0] = '<%= al.get(0) %>';
myArray[1] = '<%= al.get(1) %>';
<script>

2 Comments

Try to print out the data of al before you use this code to make sure it has values.
yaah. al contains values "txt1,txt2"
0

You could create a script tag which creates an array in js that holds all you values. I'm not familar with jsp but deppending on oyur code it could look like:

out.println("<script type='text/javascript'>");
out.println("var myJsArray = new Array();");
foreach(var arrayMember in al)
{
    out.println("myJsArray.push(" + arrayMember + ");");
}
out.println("</script>");

In Js just use the array:

function fun1()
{
    var firstArrayMember = myJsArray[0];
    // or whatever you want to do
}

You could also use assoziativ arrays if you have a key for a specific array member.

Hope you got the idea

2 Comments

Why print code to build the array, when you can just print the array literal?
As i started to write this answer there was not an other one... As I said I'm not familiar with JSP, just wanted to give him an idea.
0

I would have the Java code somewhere else, but if you insist on having it inside your JSP, try having readable HTML and only output Java content using <%=...%> - something like this:

<%
  ArrayList<String> list = new ArrayList<String>();
  list.add("text1");
  list.add("text2");
%>

<body>

  <form ...>
    <input type="text" id="<%=list.get(0)%>" />
    <input type="text" id="<%=list.get(1)%>" />
    <input type="button" id="button1" value="Click" onclick="functionOne();" />
  </form>

  <script type="text/javascript">
    function functionOne() {
      alert(document.getElementById("<%=list.get(0)%>").value);
    }
  </script>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.