Andrei Ivanov
Andrei Ivanov

Reputation: 311

Passing an array from jsp to java script

I am working on this website and came across some difficulties. I connect to the database using JSP, and display the data using JAVASCRIPT. So I want to transfer an entire array from JSP part of a code to java script part of the code. I tried to transfer a string - and even that I failed.

Of course a seached the web and seems like it is not that easy to do, just as well as to connect to database using java script.

Here I want to copy the values from int[] intArray={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} - created an array to try without loading values from database in JSP to var array[]; in JAVASCRIPT.

Or maybe there is an easier way to copy data from database to highcharts!? Please help!!!

small part of JSP code:

<%!
String jdbcDriver = "org.postgresql.Driver";
String jdbcURL    = "jdbc:postgresql:IrishClimateData";
String user       = "postgres";
String password   = "postgres";
int[] intArray={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

%>

Java Script:

$(function () {
var chart;
var array[];
var arr1 = [-2, 8, 5, 11, 17, 22, 24, 24, 20, 14, 8, 2];
$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'line',
            marginRight: 130,
            marginBottom: 25
        },
        title: {
            text: 'Monthly Average Temperature',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: WorldClimate.com',
            x: -20
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            title: {
                text: 'Temperature (°C)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function() {
                    return '<b>'+ this.series.name +'</b><br/>'+
                    this.x +': '+ this.y +'°C';
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: [{
            name: 'Tokyo',
            data: arr1
        }, {
            name: 'New York',
            data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
        }, {
            name: 'Berlin',
            data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
});

});
</script>

Upvotes: 0

Views: 5220

Answers (6)

rohit narang
rohit narang

Reputation: 135

te right answer is- abc.jsp

<html>
function call()
{
var name="xyz";
window.location.replace("abc.jsp?name="+name);
}
</script>
<input type="button" value="Get" onclick='call()'>
<%
String compose1=request.getParameter("name");
out.println(name);
%>    </html>  

it works in my program, hope you it will help you too...

Upvotes: 0

Rhys
Rhys

Reputation: 2877

I have answered this question in detail at the below link. Hopefully it helps.

Display Highcharts data from mysql database on jsp page

Upvotes: 1

Neron
Neron

Reputation: 1578

U can write something like this maybe:

first assign the arrays's elements to a string with commas like:

String a="1,2,3,4,5";

then assign it to another variable:

var arrayStr=<%=a%>;

then do this:

var array=arrayStr.split(',');

hope it works

Upvotes: 0

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

First, load your java array from the database. I just hardcoded {1, 2, 3} for the sake of argument.

int[] a = {1, 2, 3};

Print this to the javascript variable array:

<script type="text/javascript">
<%
    out.println("var array = " + a);
%>
</script>

Now you can use array in your javascript.

Upvotes: 0

errieman
errieman

Reputation: 1949

Try using JSON, it is pure javascript syntax so when you receive it in javascript you can use it as a javascript object.

JSON: http://en.wikipedia.org/wiki/JSON

JSON in jsp: Creating a json object in jsp and using it with JQuery (it is not necessary to use jQuery)

Upvotes: 0

Vasu
Vasu

Reputation: 159

Assigne the intArray to var array[] in java script using scriptlets like var array[] = "<%=intArray%>";

Upvotes: 0

Related Questions