title: Calling java class from javascript written in html
body: <p>I have a .java class to read a mongodb collection and copy the contents into the .json file.</p>
  
  <p>.json file is then provided to the chart (created using Fusion Charts as it only accepts data either in json format or in xml format) written in javascripts in html and then the chart is build on the basis of the data provided to the chart.</p>
  
  <p>My requirement is to call a java class from java script so that it will get the data in .json file and chart would get created based on it.</p>
  
  <p>How to do so?</p>
  
  <pre><code>    public class Write {
  
      //@SuppressWarnings("null")
      public void connect() throws Exception{
  
          MongoClient mongo = new MongoClient( "localhost" , 27017 );
          DB db = mongo.getDB("Age");
          DBCollection collection = db.getCollection("agegroup");
  
  
  
          try{
  
                  File file = null;
                  String content = "{ "+
        "  \"chart\": { \n"+ 
  
            "      \"caption\" : \"Weekly Sales Summary\" ,\n"+
             "  \"xAxisName\" : \"Week\",\n" +
                "  \"yAxisName\" : \"Sales\",\n"+
               " \"numberPrefix\" : \"$\" \n"+
          "},\n"+
  
           "\"data\":[\n";
  
                  // if file doesnt exists, then create it
                  file = new File("C:/Users/ila/Desktop/pie.json");
  
  
                  file.createNewFile();
  
  
                  FileWriter fw = new FileWriter(file.getAbsoluteFile());
                  BufferedWriter bw = new BufferedWriter(fw);
                  bw.write(content);
  
                  DBObject s;
  
  
                  String str;
                  String delimiter=",";
  
                  DBCursor cursorDoc = collection.find();
                  while (cursorDoc.hasNext()) {
                      s=cursorDoc.next();
  
                       str=s.toString();
                       String[] str1= str.split(delimiter);
  
                              bw.write("{\n");
                              bw.write(str1[1]+", \n");
                              bw.write(str1[2]+"\n");
  
                              if(cursorDoc.hasNext())
                              bw.write(",");
  
                              System.out.println(Arrays.toString(str1));
                  }
  
                  bw.write("]\n");
                  bw.write("}\n");
  
                  bw.close();
  
                  System.out.println("Done");
  
            }catch(Exception e){
               e.printStackTrace();
            }
  
      }
  }
  </code></pre>
  
  <p><strong>.html code</strong></p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Pie chart&lt;/title&gt;
  &lt;script type="text/javascript" src="fusioncharts/fusioncharts.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="fusioncharts/fusioncharts.charts.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="fusioncharts/themes/fusioncharts.theme.zune.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
  FusionCharts.ready(function(){
  
      var revenueChart = new FusionCharts({
       //type: "pie2d",
       type: "pie2d",
        renderAt: "chart-container",
        width: "430",
        height: "450",
        dataFormat: "json",
  
     events: {
         'slicingStart': function(evtObj, argObj){
             var label = argObj.data.categoryLabel;
            //  alert(label);
              var n= label.localeCompare("Teenage")
              if(n==0)
                  {
                     alert("An alert Msg!!!Teenage");
                   //  document.getElementById('iframe1').contentWindow.location.reload();
  
  
                   parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
  
                  }
            //  document.getElementById("iframe_a").contentDocument.location.reload(true);
  
            var m= label.localeCompare("Child")
               if(m==0)
                  {
                     alert("An alert Msg!!!Child");
   parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
  
                  }
  
            var l= label.localeCompare("Adult")
               if(l==0)
              {
                 alert("An alert Msg!!!Adult");
  parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
  
              }
  
            var p= label.localeCompare("Senior")
               if(p==0)
              {
                 alert("An alert Msg!!!Senior");
  parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
  
              }
         }
         }
    });
    revenueChart.setJSONUrl("pie.json");
   //  revenueChart.setJSONData("C:/data/db/Age.json");
       revenueChart.render("chart-container");
     }
  );
  
  
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="chart-container"  &gt; &lt;/div&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>