简体   繁体   中英

How to run a java program through a JavaScript page?

I have a java program to scan vehicle's number plate and i want to call this program through a JavaScript page ie When I click a button on my JavaScript page it should execute my java program . I know there are similar questions on stackoverflow, but none was clear enough for a beginner like me to understand. New to JavaScript, any help would be highly appreciated. Thank you in advance.

While the answer of "No" is technically correct based on the phrasing of the question. You may want to read up on AJAX. It is a way for javascript to make a request to your backend code (in this case Java).

Javascript is client side, meaning it is run by the user's browser. Java is running on your server. In order for the client side javascript to interact with the backend Java, you need to make a request to the server.

You can do it with AJAX . Javascript is client side, meaning it is run by the user's browser. Java is running on your server. In order for the client side javascript to interact with the backend Java, you need to make a request to the server. A simple example would be something like this

$.ajax({    
        type: 'POST',                 
        url: 'http://localhost:8080/MyMethod',
        data: JSON.stringify({"string" : "anything you want to send to your method"}),
        contentType: "application/json",
        error: function() {
            alert("Failed");
        },
        success: function() {
            alert("Success");
        }
    });

That depends on where you would like to run it on.

1.client side

The only method to get java codes running directly on client side, is to use a java applet. Write an applet,write your html properly, then you are all set.

Or, you may want a wasm/javascript compiler for java.

2.server side

you should setup a mechanism letting your frontend to raise the backend.

for frontend, you should be able to send certain requests. you can choose http request, aka XHR/AJAX, or, you can choose web socket. they are similar things.

For backend, if you let your httpd handle the very request, then you should have your httpd notify your code for that. The solution if different for different httpds.

If you want to handle the request directly, then you can just listen to the very port and do the regular things. You should be responsible for security issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM