0

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>
3
  • 3
    You can just print text from your jsp. Javascript is text as well. So, just print the array with the proper javascript syntax. Commented Mar 28, 2013 at 9:25
  • Thanks for reply! I dont want to print the text, I need to pass the values from JSP and display them using java script. The values have to come from JSP, since they will be loaded from database - here I created an array myself to simplify at first! Commented Mar 28, 2013 at 9:30
  • Have you tried to export data from JSP as JSON ? Commented Apr 2, 2013 at 11:14

6 Answers 6

1

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

Display Highcharts data from mysql database on jsp page

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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)

Comments

0

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.

2 Comments

Thanks for your reply, I was able to use out.println("var array = " + a[0]); to copy one element and print it ous, since ("var array = " + a) did not work. However I was no able to copy an array... I tried this: for( var i = 0; i<12; i++){ out.println("var array["+ i + "] = " + a[i]) and simple out.println("var array[i] = " + a[i]) did not work either... Is there any way to copy an entire array? Do you know? Thanks!!!
@Andrei Ivanov I can't test this now, but can you try ("var array = " + Arrays.toString(a))?
0

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

Comments

0

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...

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.