I have a java program to scan vehicle's number plate and i want to call this program through a JavaScript page i.e. 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.
Related
I'm totally new to jQuery and javascript. Not sure if this is a valid question or not. I was reading the source code for the website I'm currently maintaining. At some places in the code, I need to get the responses from users and send them to a PHP file for data processing and return the result. An example may be as below.
$.ajax({
type: "GET",
url: "get_date.php",
data: {month: $('#month').val(), year: $('#year').val()},
dataType: "json",
success: function(response) {
console.log(response.date)
}
....
})
In the get_date.php file I can write something like this:
...
echo json_encode($output)
Now I want to replace the get_date.php with another javascript file, but I don't know how to return a JSON object like what I did in the get_date.php file. I tried the following code but it doesn't work.
var output = JSON.stringify(js_object)
console.log(output)
Thanks for all the replies! The reason I want to use a js file here is I need to manipulate some HDF5 files specified by the users. As far as I know, php cannot handle HDF5 files easily, but javascript can. I hope this makes the problem clearer.
Any comments would be appreciated!
Okay, here's where you're stumbling:
"Now I want to replace the get_date.php with another javascript file"
You see, the $.ajax(...) construct in your source-code sends an asynchronous request to an external host, not "the same computer." Your computer will construct and send an HTTP(S) request to that computer, then proceed immediately with the next JavaScript statement in the program.
However, at some (unpredictable) future time, the host will send its reply. When it does so ... (hence: "asynchronously ..." the "A" in AJAX ...), and assuming that the request was 200 OK, your JavaScript program will execute the success: function.
The "new" scenario that you describe is fundamentally different from this – a JavaScript file runs on "your own" computer (therefore: "synchronously"), not "a different" one. And you simply can't swap one for the other. You'll have to re-think your design.
Of course, the community here will be happy to assist you. Tell us more ...
If you want to process files, stored on the client, with client-side JavaScript, then you need to be reading them with the file API and not involving Ajax at all.
If you want to run JavaScript server side, then you need to configure your server to run it (or replace the server with one designed to run JavaScript (Node.js + ExpressJS is a common choice)). You can't just submit an HTTP request to a JavaScript file that the server is configured to treat like any other static file for delivery direct to the client.
Hi guys,
I'm not familiar with web server, client and AJAX. I encountered redirect problems on Kitura.
The delete route can redirect to "/api/v1/users/list" succeeded.(I saw a message through print function)
but the browser doesn't reload data(refresh) for /api/v1/users/list.
Please following code, Thanks!
Q1-0)Do I need to perform a manual refresh for browser?
Q1-1)If I should, which side is better for that? (server side or browser side)
Q2)Do I need to do refresh action by manual, when I using AJAX delete method?
Server side method "delete"
---------------------------
...
router.delete("/api/v1/users/delete/:id" ....
_ = try? response.redirect("/api/v1/users/list", status: .seeOther)
...
Server side method "get"
------------------------
...
//list all users.
//each user have a delete button that performs AJAX delete method to "/api/v1/users/delete/:id".
router.get("/api/v1/users/list", ...
print("get /api/v1/users/list")
...
Short answers:
Q1-0: In your case, yes.
Q1-1: In your case, browser.
Q2: In your case, yes.
Longer answer:
This really depends on the architecture of your app:
Client/server: You build an API that sends/receives JSON or XML through REST endpoints. On top of that, you build a JavaScript client that uses AJAX to communicate with this API. This is what you seem to be doing. However, your AJAX requests should only send/receive JSON or XML data. Any page updating, reloading or redirecting should happen client-side.
Server-side: Here, most of the logic happens on the server. You use HTTP GET and POST to request pages and submit forms. The server then processes these requests and returns an HTML page for the browser to render. See https://github.com/svanimpe/swift-blog for an example that uses Kitura and Stencil.
Client/server is more flexible as you can build several clients (web as well as native apps) for the same API, but is also more complex, as it's a distributed architecture and usually involves multiple programming languages and some code duplication.
Server-side apps are generally easier to build for beginners as they are monolithic and involve very little non-Swift code (in your case).
I am trying to write a plugin which will work a lot with my server. Every page load will invoke an AJAX call to my server for data, the server should return a simple string.
Now I am trying to understand what would be the best aproach for this type of program.
Should I just create an AJAX call every time I need the data or is there some method I could create an open connection (despite the change of webpages) to save on server power?
Should I somehow listen to some port or something of the sort?
Do I have other options or what should I do to do this the most efficient way?
You can use HTML5 websockets (http://www.html5rocks.com/en/tutorials/websockets/basics/)
If you use this approach, then you will need to re-think the way you program your webserver, since websockets don't follow the request-response paradigm AJAX do. Instead they use a connection to stream data so you will need to open a port on your server and listen to it, the way to do it depends on the language or framework you are using. This is fast and responsive but will only work on most modern browsers.
Other approach is using Long Polling (http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery). This is used by some chat clients. It works sending an AJAX request to the server, the server receives it and keeps it waiting until the data is available and then the response is sent. Then the client makes another request, waits and repeats.
Probably you will almost never want to send simple strings to the client. It's almost always better to use XML or JSON to encode the response.
Just create a simple AJAX call and put it on each page, or save it as it's own file and put a server include on each page in the header. Simple as that!
$(document).load(function(){
$.ajax({
type: "POST",
url: "/where_your_string_is.php",
success: function(msg){
$("#stringHolder").html(msg);
}
});
});
Websockets API allows bi-directional communication, but I've just found that there's another option called HTML SSE that might be used if you only need to pull data. So if you've stumbled upon this question, consider this option as well.
I want tto make a chat on my site.
Very basic I want people to login to chat. And when they do that I show them the last 5 messeges.
When the person is writting something it is put into the database, and then reloads the site, with the new text from the database. So it only works when the user writes something, because it will only update when he press 'Write'.
To make it even better I am thinking of making a javascript to look up the content of the database and every 3-5 seconds.
Is that the right way to do it or is there a better way??
a lot of chat services on websites use java or flash rather than javascript, the reason is that those languages provide socket support which means they can have a permanent open connection to the server for updates.
with javascript you have to poll the server at a regular intervals using ajax or comet which is a technique for long polling, but it does have to re-establish connections every now and then.
when html5 is more widespread you will be able to use web-sockets to listen to the server for updates, but for now ajax or a flash based plugin (even to just provide sockets for js to use) is the most viable option.
something like this will provide a socket-swf-js type bridge to talk to your server
http://code.google.com/p/jssockets/
Yes, recently i've made a simple groupchat application with javascript and php and i used to check the text file where all the chat messages i'm writing to for every 2 secs....
<div id="chatbox"></div>//html div element where i've to paste the message data
$("#submitmsg").click(function(){
$.post("post.php", {text: send_mymsg});//where am sending my data to a php file to write into a html file "log.html"
}
function loadLog(){
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html);
});
}
setInterval (loadLog,2000);
Did not have luck with these examples:
Javascript File remove
Javascript FSO DeleteFile Method
Deleting a File
There are no special permissions on the file.
Is there a way to do this in JQuery?
The requirement is - a certain file must be deleted from the web directory when another page is loaded. There is no security issue as this is on a closed network.
Any help is appreciated.
Thanks.
With pure JavaScript, it can't be done. Using an AJAX call to a server side script that deletes the file would work though.
Javascript cannot delete files, it is prevented as it would lead to HUGE security vulnerabilities. THose links are for ActiveX controls that are handled through JS. Use a server side language.
You can't delete files over HTTP (well in theory you can, but it's not implemented.)
The easiest way is to set up a tiny server side script (e.g. in ASP or PHP) and to call that from JavaScript. The server side script needs the proper permissions to do the deletion, but otherwise there is no problem.
In PHP the start would look like this: (Not expanding solution to a fully secure one because you're not saying what platform you are on)
<?
// STILL INSECURE!!!!
// Do not use in any public place without authentication.
// Allows deletion of any file within /my/files
// Usage: filename.php?file=filename
$basedir = "/my/files";
$file_to_delete = $_REQUEST["file"];
$path = realpath($basedir."/".$file_to_delete);
if (substr($path, 0, strlen($basedir)) != $basedir)
die ("Access denied");
unlink($path);
?>
you would call the script like this:
http://yourserver/directory/delete_file.php?file=directory/filename
You cannot delete a file on a remote server using only JavaScript running in a visitor's browser. This must be done with a server-side script.
If you are doing this in a RESTFUL way, you would send an HTTP DELETE request.
jQuery's ajax method states that you can use the method parameter to specify 'DELETE' but notes that some browsers may not support it.
Obviously you will need a webserver which will accept a DELETE request, and apply some sort of authentication/authorization so that joe random visitor can't delete your files. I believe Apache's mod_dav will get you started here.
Javascript is a client side language. So you are not able to delete file on server directly. All examples that you provide may be used only for deleting files on your local machine but not into server.
But you may call some server page function that will delete file.
You can't delete files with JavaScript as it is run locally. So, it doesn't even touch external files.
You need to use a server side language that has access to editing the files such as PHP, RoR, or ASP.
You can however use jQuery to call the server side code via AJAX such as $.get or $.post and then the server side code deletes it and it would seem as though JS is deleting the files.