title: JavaScript - And Creating and using Java Objects
body: <p>I am creating a web app that requires the use of a Java Object from either a jar or class file. How do I bridge my Java class and JavaScript? (I am fairly new to JavaScript and Web Development in general).</p>
  
  <p>(Most of the search results I am getting for this topic are from people who don't realize that Java and JavaScript have nothing to do with each other. This is not the case here.)</p>
  
  <p>What I want to accomplish:</p>
  
  <p>The PropertiesEditor object first loads a properties file template and splits up the properties into an ArrayList. Then a form is generated on the webpage for each property, and allows the user to edit the values and submit with a new file name. The values are then passed to the PropertiesEditor object and the properties file is created, and saved with the other properties files. This web app will allow non programming users to create new properties files from an existing template; in my case for localization of text language.</p>
  
  <p>Java Class:</p>
  
  <pre><code>public class PropertiesEditor {
  private File propertiesFile;
  private ArrayList&lt;Property&gt; propertyList;
  private Scanner scan;
  
  /**
   * 
   */
  public void load(String fileName) {
      try {
          propertiesFile = new File(fileName);
          scan = new Scanner(propertiesFile);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
      propertyList = new ArrayList&lt;Property&gt;();
      try{
      while (scan.hasNext()){
          String string = scan.nextLine();
              if (!(string.startsWith("#"))){                 
                  String[] array = string.split("=");
                  String key = array[0];
                  String value = array[1];
                  Property property = new Property(key, value);
                  propertyList.add(property);
              }
      } 
      }catch (NoSuchElementException nse){
      }
  
  }
  
  public void save(String fileName){
      Properties properties = new Properties();
      for (Property current : propertyList){
              properties.setProperty(current.getKey(), current.getValue());
      }
          try {   
              File file = new File(fileName + ".properties");
              FileOutputStream fileOut = new FileOutputStream(file);
              properties.store(fileOut, fileName);
              fileOut.close();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  }
  
  public int getNumberOfProperties(){
      return propertyList.size();
  }
  
  public String getPropertyKey(int index){
      return propertyList.get(index).getKey();
  }
  
  public String getPropertyValue(int index){
      return propertyList.get(index).getValue();
  }
  
  public void setPropertyValue(int index, String value){
      propertyList.get(index).setValue(value);
  }
  }
  </code></pre>
  
  <p>How I'm thinking the JavaScript should look:</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
         var pe = &lt;PropertiesEditorObject&gt;;//Obviously where I want to create the Java Object
         pe.load("en-us.properties");
  
      function formValidator(){
          for (i=0;i&lt;pe.getNumberOfProperties();i++){
              var current = document.getElementById(i);
              pe.setPropertyValue(i, current);
          }
          pe.save(document.getElementById('fileName');
      }
  
      function createForm(){
          document.write("&lt;form onsubmit='return formValidator()' &gt;")
          for (i=0;i&lt;pe.getNumberOfProperties();i++){
              document.write( pe.getPropertyValue(i) + "&lt;input type='text' id='" + i + "' /&gt;&lt;br /&gt;"
          }
          document.write("Properties File Name: &lt;input type='text' id='fileName' /&gt;&lt;br /&gt;&lt;input type='submit' value='Check Form' /&gt;&lt;br /&gt;&lt;/form&gt;");
      }
      &lt;/script&gt;
  </code></pre>