Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Java and JavaScript work together?

I'll preface this by stating that I know Java is not JavaScript and vice versa.

I've got a project where I need to count occurrences of words for each of 1750 document names and document contents. I've got some awesome JavaScript from a colleague that does exactly what I want from a form input on a web page.

I want to use Java's FileReader, BufferedReader, walkFileTree, etc. to traverse the directories in which the documents live.

I'm not sure if this is the most efficient or effective approach, but both the Java and JavaScript parts of the code are working independently of one another now, and I'd like to see if I can get them to pass data between them before I start re-inventing the wheel.

Here's where I am so far. I'm stuck at the CLParse method & have inserted pseudocode:

public static void main(String... aArgs) throws FileNotFoundException    {
    File startingDirectory= new File("CGT");
    List<File> files = FileListing.getFileListingNoSort(startingDirectory);
    for(File file : files )           {
        CLParse(file.toString());
    }   }

static private List<File> getFileListingNoSort(File aDirectory) throws FileNotFoundException    {
    List<File> result = new ArrayList<File>();
    File[] filesAndDirs = aDirectory.listFiles();
    List<File> filesDirs = Arrays.asList(filesAndDirs);
    for(File file : filesDirs)          {
        result.add(file); //always add, even if directory
        if ( ! file.isFile() )   {
            List<File> deeperList = getFileListingNoSort(file);
            result.addAll(deeperList);
        }      }      
    return result;
    }

 /* is something like this doable and how would I do it?
 */
public static void CLParse(String fn) {
      pass fn to JavaScript counter
      return an array of {word,occurences} for the string
      write array to file
      }

I'll be creating another set of methods to extract and pass the document CONTENTS as a string as well. I'd love to know if anyone has any practical experience passing values back and forth between Java and JavaScript, and advice on a good/better way to do it.

like image 614
dwwilson66 Avatar asked Feb 15 '26 10:02

dwwilson66


2 Answers

You got 2 Options to let them interact with each other, which i know:

1.Applet <-> javascript

2.Serlvet <-> javascript

With option 1, you have to build a Communication with a JSObject: JSObject

or you cann call the Applets Method instanstly with document.appletname.methodname(); with this you can even Parse same simply Formats to each other.

With Option 2 you have to build a communication with a Servlet.

in here you have to send an Ajax request to the the Servlet:

$.post('login',{name:"Peter", pw:"123456"},function() 
{ 
   //do whatever
})

JavaServlet class

the first comment, has to written as an Servlet in your web.xml, it´s the servlet pattern. the second ones, are the parameters which can be read in the servlet. the function describes the stuff, which can be done in the request.

The differences between these two Options are:

1.the Applets runs on the users Computer, so you can access his files. But for this your applet has to be signed.

2.the Servlet runs on the Server. Here you have got full file access(if the system allows you too have it).

like image 56
SomeJavaGuy Avatar answered Feb 17 '26 00:02

SomeJavaGuy


I would try to investigate Mozilla Rhino.
http://en.wikipedia.org/wiki/Rhino_%28JavaScript_engine%29

like image 39
Jan Krakora Avatar answered Feb 16 '26 23:02

Jan Krakora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!