2
 <%                                                            
  HttpSession   session1 = request.getSession();
  String Marks =  session1.getAttribute("Marks").toString();
  String Percentage= session1.getAttribute("Percentage").toString();
  String marks1 =session1.getAttribute("marks1").toString();
  int total = 25;    
  int m1 = Integer.parseInt(Marks);
  int m2 = Integer.parseInt(marks1);
  float m3 =  ((m1+m2)*100)/25;
  %>

I want to store the float m3 value in a JavaScript variable.

JavaScript / jQuery code:

<script>
$(document).ready(function() { // 6,32 5,38 2,34
    $("#test-circle").circliful({
        animation: 1,
        animationStep: 5,
        foregroundBorderWidth: 12,
        backgroundBorderWidth: 12,
        percent: 92,
        textSize: 25,
        textStyle: 'font-size: 10px;',
        textColor: '#666',
        multiPercentage: 2,
        percentages: [10, 20, 30]
    })

});
</script>

I want that my JavaScript variable percent should get the value of my Java variable m3. I want this because I want to get the value in percent variable dynamic not static as I have written 92 in front of percent.

I know that on the same page you can't store a JavaScript variable into a Java variable because the JSP page is loaded first. But in my case I want to store the Java variable m3 which is in float into my JavaScript variable percent.

I have a very little knowledge of jQuery so please help!

2
  • 2
    Replace 92 with <%= m3 %>. However, please note that result of ((m1+m2)*100)/25 is an integer value, not a float value. If you want floating point, you should define variable as double and divide by 25d to force a double result. Commented Apr 5, 2017 at 23:27
  • Welcome to Stack Overflow! I've edited your question a bit. If you indent something by 4 spaces, it will become code markdown. So it's important to indent code, but also to NOT indent text. Commented Apr 6, 2017 at 6:40

2 Answers 2

1

If the script code is in your jsp page, you can simply replace 92 with <%=m3>

On the other hand if your script is in a different file loaded in by the jsp, you probably will have to store the value of m3 somewhere in the generated html (just like above) so you can pick it up again in the script.

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

Comments

0

One way to do it:

     <%                                                            
      HttpSession   session1 = request.getSession();
      String Marks =  session1.getAttribute("Marks").toString();
      String Percentage= session1.getAttribute("Percentage").toString();
      String marks1 =session1.getAttribute("marks1").toString();
      int total = 25;
      int m1 = Integer.parseInt(Marks);
      int m2 = Integer.parseInt(marks1);    
     %>

    <script>
      var m3 = <%= ((m1+m2)*100)/25 %>; // javascript line
    </script>  

Now m3 is javascript variable, accessible from rest of your code.

1 Comment

What is the purpose of Integer.parseInt(Marks)? If Marks is a string with a valid integer value, <%= Marks %> would generate same output. --- Also, you're missing ; at the end of those two JavaScript statements.

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.