so, here is the thing, I need to run .jar I've programmed myself on a server.
On the otherside, that server is also a webserver, so it displays webpages and so. Those webpages are written in simple HTML and JavaScript.
So, here is the thing:
is there a way to run my program when users perform click on links in the webpage?
I created a function in Javascript, but I cannot make it work, as long as I don't know how to run shell commands in Javascript.
The execution of the program is totally transparent to the user, and what it basically does is to search content in some documents in order to update the html that is shown to the user.
Any ideas? Hope I made myself clear.
Thanks in advance!
You really can't run shell commands in JavaScript.
If I understand your question correctly, you're looking to do some sort of remote process execution.
Is your JAR file on the other server - is it running as part of a web server, I.E. as a servlet in an application container like Tomcat, such that your code can be executed by calling HTTP methods? (If not, you may want to start out with doing this.) If so, then you'll need to have your JavaScript make the HTTP call to the server to execute its code. A common way of doing this today is through the use of AJAX - and you can use something like jQuery to help with this. The response from this AJAX request (could be XML, JSON, or pre-formatted HTML) could contain the details to "update the HTML that is shown to the user".
The simplest way to do this is put the html, javascript, and a jsp page in the same war file, and then run it on tomcat. Not the best way, but definitely the quickest.
EDIT:
So to clarify, the JSP page will get invoked by the click (ajax call, use jquery), and renders some json/xml. The success handler for the ajax call takes the jsp response and updates your html page. So the jsp runs on the server, but the html/javascript runs in the browser.
If you want to make if fancier, you can use spring web-mvc and write a controller instead of a jsp page. It has the same overall effect either way.
Related
First of all, I am not a native English speaker, so please forgive any spelling mistakes I may have.
I am trying to integrate HTML and Python to create a GUI that can "speak" to a bus (KNX bus if you are interested).
I have a raspberry pi with an HTML page and some Python scripts, that are the ones that actually talk to the bus. I have managed to run python scripts from HTML with a bit of PHP (I am not particularly proficient in PHP), doing something like this:
if (isset($_POST ['button'])){
exec("sudo pathofpythonscript/pythonscript.py")
}
And it works just fine, when the button it's pressed, the script it's executed. But now, I want to have a script running (since I want python to be reading from the UART from the bus and display that information) and when something in the scripts happens (a condition it's met, for example), I want to be able to talk to the HTML to change a CSS property or anything else, something like this pseudo-code here
//This is just pseudo-code to try to illustrate my question, IT'S NOT working code
//In the python file, a simple toggle function
if bus_event
var != var
//in Html, with javascript
if (var_from_python_script == true){
document.getElementById('button').style.background=red;
}
If someone could tell me just how to make a simple example, with something like a python script that toggles any boolean parameter that enters and returns it, and how to "grab" that return from javascript/HTML to use it,
I think I can work everything else.
Thank you in advance!
You are confusing the HTML/CSS/Javascript code that is running in your browser and the (PHP/Python) code that is running on the server. Even if in your case, they might be the same, it doesn't matter. The python script is not running in your browser and therefore cannot change the state of your html/css or even javascript.
When you are executing your code with exec, the PHP server is running an external command and expecting a response to the immediate request. After that, it's over.
I would recommend to ditch PHP and use a Python web application framework. Flask, Django or whatever else. This will allow you to interact directly with the python code. Your web browser could then request data to your server and know what is happening. Remember, the web is normally only from the browser to the server.
Now, if you need real-time interactions, you should have a look at websockets. These will allow bidirectional communication between your browser and the running python app. There are a number of caveats with this technology but it might be better suited to your needs. Note that this will force you to write Javascript code in your browser to manage this communication but it shouldn't be too hard.
So PHP is executed server-side. But is it possible for PHP to be run after the page is loaded?
To illustrate, if I had a value (stored in a file, lets say) that changed every second. When I hit a button that uses Javascript to ask PHP to get that value, does it display what the value currently is, or what it was at page load?
I think you need to get one of those diagrams that show how basic HTTP and the web server works. It will make more sense to you, than explained in plain words here.
In the simplest possible case, the result of you typing some address and getting a web page with its contents can be summed up, due to a result of process in request/response relationship between your browser and a web server located somewhere in the world.
Plain HTML
In a less simpler way, think of it like this. basically, if a page is during a refresh phase, (meaning you clicked something and are waiting for a data to comeback) then, that means it is getting/loading the response from the web server. If the web server does not have PHP installed as a module, then the only thing it is waiting/loading (in many cases) is plain HTML content.
With PHP
On the other hand, if we assume you have a file called index.php in your webserver, and have PHP is installed, in this case the web server will send everything that appears in-between <?php ?> to the PHP interpreter, first, then wait for it until PHP does its magic and send back to the server only the result.
<?php
echo 1+1;
?>
So, in the above case, the webserver (ex: Apache, Nginx) does not care what is inside the opening and closing tags, and sends the entire code to the PHP interpreter, and PHP would compute that script according the way it understands it and sends only the computed result back to the server, as plain HTML. In this case the number 2.
The role of AJAX.
AJAX (Asynchronous JavaScript and XML) is a technique used Javascript, to help you send requests and receive the response without having to load the page. This is usually done by using the browsers XHR object. So, there is no mystery in this whole shebang.
The above can be summed up simply in the following steps.
Enter foo.com browser sends a request to the server of foo.com
server/browser exchange messages server allows browser to aquire
information server sends index.php back to browser if <?php tag
is found in the script, server sends all the codes inclosed in those
tags to the PHP interpreter The PHP interpreter, compiles the query
and sends the result as HTMl
PHP is server-side script, before return page content to client side like browser, it should parse all the PHP logic into HTML logic, so it should display the value which was at page load. And if your PHP logic consumes long time to execute, it will delay the content display at client side.
Your example case is really difficult to explain, without getting technical, or using a lot of chained logic...
...however, I'll try to keep this a little more simple:
The overwhelmingly vast majority of the time, PHP will run only when something connects to the server, and will stop running as soon as it's done running the script that was accessed.
That "something" might be a page-load (//mysite/index.php), or it might be issuing an XMLHttpRequest ("AJAX") to ask the server for data (//mysite/articles.json).
Not all languages work this way.
I'm in search of some best practices. I have a webpage asp.net (a loading page as you will).
Once this page is loaded (and visible to the user) i want to execute a function that might last some seconds.
problem is, how do i launch this function?
I've tried loadComplete (event),unload (event) , working with timeout (javascript to poll itself with json) and even the timer-class (.Net). Could anyone point out an alternative/ best pratices to perform this kind of action?
This won't work the way that you are trying to do it. To understand why you have to understand the concept of webservers:
Webservers use some code (PHP, ASP.NET, ...) to generate dynamical HTML code that is beeing sent to the client. This process works in the following steps (in case of ASP.NET)
Client requests something from the server.
Server generates html (that's your ASP.NET / C# code)
Server sends HTML to client
The browser on the client receives the HTML and displays it.
So your ASP.NET code is only running in step 2. This makes it impossible to catch the event when the client has rendered his page in his browser. However there is client-side technology called AJAX. You can make AJAX calls using javascript (that runs in the browser of the client).
So for example you can create a javascript function that sends a second request to the server when the html was loaded in the client, and executes some function on the server.
For this you will need a webservice on the server that will serve as a interface between the javascript and your C# code.
A easy way to create webservices is to use WCF webservices
If you have any further questions feel free to comment on this answer.
I'm wondering how I can embed Rails code inside an html.erb so that I can run a Ruby function on the press of a button. I've googled around, and if this is not possible, how could I run a JavaScript function that updates variables inside a Ruby on Rails database?
I think we need to talk a bit about how the web works. No worries; this is confusing for a lot of people when they first start building web apps.
There are two types of code here. The first type is the server-side code, which runs on your server. In this case, it's Ruby code, and the user can't see it. This server-side code generates the second type of code, client-side code. In this case, it's Javascript code, since that's what browsers understand. The user can see it and even modify it, and it runs on the user's computer.
Since the database lives on your server, and is not publicly accessible (after all, wouldn't that be pretty bad?), you will have to use server-side code to interact with the database. Code that runs on the user's browser cannot directly interact with the database, so you'll have to use a kind of "glue" technology here.
One way of doing that is an AJAX request. This is when your Javascript code tells the browser to send a message to your server and wait for a response. This could happen on, say, a button click. Then, the server can handle the request as usual, interact with the database, whatever, and send the result back to the browser. To the user, however, all they see is that clicking the button takes the action they expect, and that's all that matters.
There are a few ways to go about implementing AJAX in a Rails application. Rails has a few built-in methods to look into, like button_to_remote in Rails 2 or button_to with the :remote option in Rails 3, that can help simplify the process. Or, you can write the AJAX code yourself, and even Googling "AJAX tutorial" would be a good starting point. I'd start with the Rails helpers, though, since they usually make things pretty simple for beginners.
Good luck!
You could use an JS AJAX request to request a backend ERB file, which then accesses the database.
Using AJAX with JS
Using AJAX in jQuery
I am currently having an idea where I want to save an image from a c++/openGL application on demand from a browser. So basically I would like to run the application itself on the server and have a simple communication layer like this:
JS -> tell application to do calculations (and maybe pass a string or some simple data)
application -> tell JS when finished and maybe send a link, text or something as simple as that.
I don't really have alot of experience with webservers and as such don't know if that is possible at all (it's just my naive thinking). And note: I am not talking about a webGL application, I just want to have simple communication between a c++ serverside application, and the user.
Any ideas how to do that?
Thanks alot!
Basically no matter what your language/framework you choose for your web server, you just need a interface that is callable from your browser JS, and you can do whatever you want in the server once it recieves the call.
Most likely any web service interface exposed from the server.
Just need to safeguard your server not to get DoS since it sounds like it's a huge process.
As far as I know, JavaScript (at least when embedded in HTML) is executed on your local machine and not on the server so that there is IMHO no way to directly start your server-application using JS.
PHP for example is executed on the server-side and so you could use e.g. the php system function to call your C++/OpenGL application on the server - initiated on demand through a web-browser.
When the call is finished you could then directly present the image.
Well you could always use the cgi interface to invoke your application
and have it save that image somewhere accessible to the webserver.
Then have your js load that via ajax.
Or make a cgi App that talks to the app and then serves a small
page with the pic in it.
[EDIT]
Answering the comments:
CGI is not complex to learn, it is mostly a simple convention
you can follow. I think it would give you the maximum of
flexibility. I don't know which php mods allow you to leave the cozy protection of the server-application and interact with other stuff on your server.