Friday, 8 December 2017

JavaScript replacing CGI

Java is a programming language and JavaScript is a scripting language. Java programs are compiled on the server.Java programs are compiled on the server.

You can write stand-alone programs in Java. Scripts written in JavaScript are interpreted by the browser. You cannot write stand-alone programs in JavaScript-you need a browser to interpret JavaScript.

Java is object-oriented. It employs classes and inheritance. It provides encapsulation of data.

But the JavaScript is object-based. There are no classes. There is no inheritance. Data within objects is readily accessible.

Java requires that data types be strongly typed.If a function expects one of its arguments to be a number, the function will not accept a character string.
JavaScript is loosely typed. JavaScript has numbers, character strings, and Booleans and freely interchanges them.

The Common Gateway Interface (CGI) provides a mechanism for a program on the server to interact with the client's browser. You can use any language to write CGI programs, and CGI programs may be interpreted (PERL scripts, for instance) or compiled (C or C++).

JavaScript can do a lot of the same things CGI can do, and it can often do them much more efficiently. For example, JavaScript can do form validation more efficiently than CGI.

When a non-JavaScript page has user input fields, it sends all the field values to a CGI server application. The CGI application then has to figure out whether the data in each field makes sense before doing something with the data.

 A JavaScript page, however, can validate the data entered before it is sent to the server. If the data is invalid, JavaScript can block transmission to the server.
Because all of this work is performed on the client side, JavaScript does not waste bandwidth transmitting bad data and then receiving an error page from the server.

JavaScript can also replace some of the animation and page-reloading functionality of CGI. To perform animation and page-reloading, CGI provides mechanisms called "server push" and "client pull."

With server push, the Web page server maintains a connection between the client and server. Server push restricts the number of simultaneous connections the Web page server can maintain-a popular page using server push will frequently reward potential visits with a "sorry, not now, try later" message. Client pull, on the other hand, involves the client frequently re-establishing its connection to the server, artificially adding to the traffic at the server.

One popular use of CGI is in hit counters-programs that modify the page to show how many times that page has been visited. Another popular use of CGI is in form handling, where a program on the server reads the data from the user input fields and does some work based on that data.

JavaScript, which does its work in the client's browser, cannot entirely replace CGI. For instance, a hit counter has to update a file on the server so it can remember how many times the page has been visited by all visitors.

That's a little difficult for JavaScript, but a JavaScript Web page can keep track of how many times a given visitor has visited the page. So can CGI, but only if given an endless supply of disk space on the server.

You can use JavaScript to create dynamic documents that would have required either server push or client pull in CGI.

You are absolutely correct but that would involve no additional traffic or long drawn-out connections between the client and the server.

Some web space providers only allow the use of a limited set of applications. Many providers do not support server push CGI.

JavaScript running on the client browser is perfectly safe to the server, and affords you, the creator of the JavaScript document, much greater flexibility in how your document interacts with the reader.