Home arrow JavaScript arrow Page 4 - Active Client Pages at the Server
JAVASCRIPT

Active Client Pages at the Server


Active Client Pages produce web pages at the client computer using web technology. The programming analysis, logic and calculations of the data for the pages are done at the client. The data to create the pages resides on the server. So what is the role of the server? That is what this article addresses. You need basic knowledge of JavaScript and Perl to understand it.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
October 20, 2009
TABLE OF CONTENTS:
  1. · Active Client Pages at the Server
  2. · The first thing that happens at the server
  3. · Using Perl to send a string to the client
  4. · Obtaining a string from any file in any directory at the server

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Active Client Pages at the Server - Obtaining a string from any file in any directory at the server
(Page 4 of 4 )

We said above that if you are using Ajax, the first thing that will happen in the server is that a Perl file will be executed. This executing Perl file may have to search for the string in a file (text), in a directory at the server that is different from the root directory. I show you how to achieve that in this section.

Well, I will give you a very basic explanation. A full explanation would call for a whole article. I will just show you how you can read the content of a whole file in a directory at the server and send it to the browser.

Let us refer to the directory at the server that has the executable Perl file as the current directory. This may be the cgi-bin directory. In this section, I show you how you can read the content of a whole file from this directory or from a sub directory of this directory. What I show you here will work with a text file or an HTML file; just replace the text file in the example below with an HTML file.

A filehandle

A filehandle is a special type of Perl variable that is associated with an output destination or input source. It is used to tell your program where you want output to go or input to come from. In our case, we want input to come from a file in the current directory or a sub directory to the current directory.

The following code, which is in the executable Perl file, will read the content of the  input.txt file in the same directory as the executable Perl file, and send to the Ajax function in the browser at the client (see explanation below).

open(INFO, "< input.txt") || die("can't open datafile: $!");

while ( <INFO> )

{

print $_;

}


close INFO;

The first statement creates and opens a filehandle called INFO. This file handle will represent the input.txt file. The first occurrence of INFO is in the first argument of the open() function. This function can be used to create and open filehandles.

There are two arguments in the function. The first one is the INFO filehandle. The second argument for the open() function is a string. There are two parts to this string. First you have the "<’"character. This character indicates that you are opening a file for reading. Next in the string, you can have a space of any length. After that, in the string, you have the name of the file. When there is no path associated with the file, as in the above code, it means that the file is in the current directory -- the same directory as the executable Perl file.

There are two functions in the first line of the code above, the open() function and the die() function (see explanation below). In between these functions you have the logical OR operator, ||. If the open() function fails, the die function sends an error message to the standard location where errors are sent; I will not say more than this about the die() function in this article.

If the opening of the filehandle is successful, the statements in the code below the first line will be executed. After the first line, you have a while-loop. In the while-condition you have the following expression:

<INFO>

The angle brackets and the filehandle inside the angle brackets copy the next line (terminated by the new-line character) in the file for each iteration, beginning of course from the first line. During each iteration, this next line is copied into the default variable, namely $_. For each iteration, the print statement sends the line of text to the Ajax function at the server. The Ajax function receives all the lines in one call.

After opening a filehandle, the filehandle has to be closed. You close the filehandle after using it. To close a filehandle, you use the “close” function as shown in the code above.

So the above code has to be in the executable Perl file called by Ajax from the client.

Now if the file is in a sub directory of the current directory, then you have to precede the file name in the open() function with the sub directories, separated by forward slashes. Let us say you have an immediate sub directory to the current directory called "subdir" and the file is inside this sub directory. The first line in the code would be:

open(INFO, "< suddir/input.txt") || die("can't open datafile: $!");

Getting content from the database

You can use Ajax at the client browser to get a string from a table cell of a database at the server. You need to know what to do at the server to get the string from the database. The explanation of what you have to do needs more than one article. I will not do that here. You can consult books or search for tutorials on the Internet.

With Active Client Pages, the analysis, logic and production of web pages for the web session are done at the client. You need just what I have explained in this article for the server. Well, as I said above, when it comes to databases, you still need to learn how to get data from the database tables; however, the bulk of the programming is at the client.

I hope you appreciated this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials