1

I have a JSP page called index.jsp. I have a Java variable called totalCount inside this page :

<%= int totalCount = getTotalCount();%>

Now I want to use this variable in Javascript section to generate a chart:

<script type="text/javascript">
</script>

How can I pass this Java variable inside ? Thanks.

hawarden_
  • 1,904
  • 5
  • 28
  • 48

4 Answers4

5

Just assign the value to your Javascript variable.

<script type="text/javascript">
var count = '<%= totalCount %>';
</script>

But using scriptlets been highly discouraged and shift to JSTL as soon as possible.

Read : How to avoid Java code in JSP files?

As others pointed out, do not create unnecessary hidden elements on DOM. If you really want to use this variable across files declare this variable on top. Even before script includes, so that it avail across all the files.

Example:

 <script type="text/javascript">
    var count = '<%= totalCount %>';
    </script>
<script type='text/javascript' src='js/blah.js'></script>     
<script type='text/javascript' src='js/blahblah.js'></script>     
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

Just create a hidden field on your HTML page and access that value using JavaScript.

HTML

 <input id='hdn-total-count' type='hidden' value='<%=totalCount%>' />

JavaScript

 var totalCount = document.getElementById('hdn-total-count').value;

jQuery (Cross browser compatible)

 var totalCount = $('#hdn-total-count').val();

All of the other answers are going to work on inline and on-page JavaScript. But they are not going to work for JavaScript in external files (which are cacheable).

Allan Chua
  • 9,305
  • 9
  • 41
  • 61
  • +1...or you can use data-* attribute to store the value on any element instead of using a separate hidden field. – bhavya_w Aug 24 '15 at 10:10
  • @bhavya_w Why to create an unnecessary DOM element here ? instead of a script variable. Any special benefits ? – Suresh Atta Aug 24 '15 at 10:12
  • @sᴜʀᴇsʜᴀᴛᴛᴀ.. can you use "<%=totalCount%>" in an external js file ? Actually we follow a convention of not writing any js on jsp pages and have separate (external) js files for all javascript code. – bhavya_w Aug 24 '15 at 10:16
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ, someday you will understand ;) – Allan Chua Aug 24 '15 at 10:21
  • @bhavya_w Why not ? Look at my edit. – Suresh Atta Aug 24 '15 at 10:25
  • @AllanChua - Well, have my edit to the answer. – Suresh Atta Aug 24 '15 at 10:26
  • 2
    @sᴜʀᴇsʜᴀᴛᴛᴀ, still bad. Your answer augments the global scope and mitigates the chance of caching the variable on external file. It also slows down the scope resolution on the global scope. – Allan Chua Aug 24 '15 at 10:28
  • 1
    @bhavya_w Why I have to scatter my scriptlets everywhere in other js files where I have script variable in my hand available ? – Suresh Atta Aug 24 '15 at 10:30
  • @AllanChua Yes of course. I make it global variable that doesn't mean that it avoid all the cons of them. – Suresh Atta Aug 24 '15 at 10:32
  • 2
    @sᴜʀᴇsʜᴀᴛᴛᴀ...sir imho you have a variable in hand and you can use it anywhere but when you are working on large scale project you should follow this convention of not writing your scripts within the html/jsp pages..please go through this http://programmers.stackexchange.com/questions/134953/when-are-scripts-inside-html-not-considered-a-bad-practice....also this http://programmers.stackexchange.com/questions/270723/why-is-it-a-good-practice-to-keep-javascript-code-in-separate-files – bhavya_w Aug 24 '15 at 10:36
1

simply you can use

<script type="text/javascript">

    var xyz=<%=  getTotalCount();%>
</script>

But I dont recommend you to use java codes inside JSP.Please look for JSTL

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

you can write a code something like this. But it's not a clean way of doing it. Why do you want to do it? You can do it via AJAX calls.

Here is the sample code

<sciprt type="text/javascript">
  var myVariable = <%=request.getAttribute("rep");%>
</script>
Hitesh Kumar
  • 643
  • 1
  • 6
  • 22