0

Hi I am trying to set jsp variable into a javascript array. I tried, but I am not able to achieve this when I run my code. I do not get my output, I only see a blank screen.

Here is my code:

value.jsp

<script>
             <%

       URL url;
         ArrayList<String> list1 = new ArrayList<String>();
            ArrayList<String> list2 = new ArrayList<String>();
           List commodity = null;
           List pric = null;
           int c = 0;
           int p = 0;

     try {
            // get URL content

            String a = "http://122.160.81.37:8080/mandic/commoditywise?c=paddy";
            url = new URL(a);
            URLConnection conn = url.openConnection();
            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(
                               new InputStreamReader(conn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String inputLine;
            while ((inputLine = br.readLine()) != null)
   {
                    System.out.println(inputLine);
                  //  sb.append(inputLine);
                   String s=inputLine.replace("|", "\n");
                   s = s.replace("~"," ");
                   StringTokenizer str = new StringTokenizer(s);
                    while(str.hasMoreTokens())

   {
       String mandi = str.nextElement().toString();
       String price = str.nextElement().toString();
       list1.add(mandi);
       list2.add(price);
   }

   }

       commodity = list1.subList(0,10);
                         pric = list2.subList(0,10);
                         for (c = 0,p = 0; c < commodity.size()&& p<pric.size(); c++,p++) 
            {
                String x = (String)commodity.get(c);
                String y = (String)pric.get(p);
                //out.println(y);
                //out.println(x);}
             %>



     jQuery(function ($) {
    var roles = []; 
     roles.push(<%= x%>)  
     roles.push(<%= y%>)  
     <%

            }
     %>

    var counter = 0;
    var $role = $('#role')
    //repeat the passed function at the specified interval - it is in milliseconds
    setInterval(function () {
        //display the role and increment the counter to point to next role
        $role.text(roles[counter++]);
        //if it is the last role in the array point back to the first item
        if (counter >= roles.length) {
            counter = 0;
        }
    }, 400)
    });

    </script>

            <%

     br.close();

            //System.out.println(sb);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  

    %>

How can I achieve my desired output?

Illidanek
  • 996
  • 1
  • 18
  • 32
user3585120
  • 15
  • 2
  • 8

3 Answers3

1

When you output to JS from JSP, you have to make sure your output is valid JS.

Your current output will output a string with no quotes, which the JS interpreter will treat as JS code and not strings or arrays as you want.

In addition, you are producing 10 copies of the JS

jQuery(function ($) {
var roles = []; 
roles.push(<%= x%>)  
roles.push(<%= y%>) 

and that function is never closed.

I would look for a decent JSON output library. That would simplify your life quite a bit here.

bob0the0mighty
  • 782
  • 11
  • 28
  • [JSON](//en.wikipedia.org/wiki/Json) is basically a human readable object notation, I would say like XML. Here's a useful Java library that should help you with actual implementation, [GSON](https://code.google.com/p/google-gson/) – bob0the0mighty May 02 '14 at 00:05
0

It looks like you are clearing the roles variable var roles = []; with each pass of the loop.

You might want to consider not posting the entire file, but pairing the example down to just the part you need help with.

You could also put the array into a JSON object and parse that in your javascript. That might be a cleaner implementation.

BamaPookie
  • 2,560
  • 1
  • 16
  • 21
0

try enclosing the scriptlets with quotes like roles.push("<%= x%>") and also check the array initialization.

vishy12
  • 47
  • 4