Client/Server Programming -- Week 14
Introduction
This week we'll cover some non-Java client/server technologies.
Note that the links from this page are to handouts that will be distributed the night of class. Only print out those handouts if you could not attend class.
Main topics this week:
Assignment 9-1 Due
Assignment 12-1 Due
Client-Side Scripting
An Overview of non-Java Technologies
PHP
Variables in PHP
Functions
Including Other PHP Files
Template Engines for PHP
PHP and Databases
Review for Final Exam
Next Week
The research paper comparing Microsoft and Java technologies is due today for full credit.
The JSP assignment is due today for full credit.
We've talked quite a bit about the server side of a web application, and not much about the client side. So we'll take just a few minutes to mention that you can also put scripting into the web page to be executed on the client's machine, using JavaScript. JavaScript can execute code when the user moves the mouse over an item on a web page, or when the user clicks a button on a web page, etc.
For example, consider a form that you have asked the user to fill out. Perhaps there are sets of predefined answers that the user might be expected to type into the form. Rather than force the user to type in these predefined answers, you could provide a button on the page that would automatically fill in the predefined answers. That would look something like this:
<form>
<table>
<tr>
<td>
<input type="button" name="button" value="Spouse" onClick="set_relation ('Spouse');">
</td>
<td>
<input type="button" name="button" value="Child" onClick="set_relation ('Child');">
</td>
</tr>
</table>
</form>
This form provides the buttons the user can click...note that the form has no action, so this is not intended to interact with any other web page. Instead, when the user clicks one of the buttons, the onClick code is called, which executes set_relation.
<SCRIPT LANGUAGE="JavaScript">
function set_relation(n)
{
document.newform.relation.value = n;
}
</SCRIPT>
The JavaScript code for set_relation simply sets document.newform.relation.value equal to whatever was passed in. Let's look at the newform form:
<form action="set_relation.php" name="newform" method="POST">
<table>
<tr>
<td align="right">Name:</td>
<td><input type="text" name="name" size="70"></td>
</tr>
<tr>
<td align="right">Relation:</td>
<td><input type="text" name="relation" size="70"></td>
</tr>
</table>
</form>
We've done something new with this form, we've given it a name, "newform". This allows the JavaScript to get to it to modify it's contents. The JavaScript will modify the value of the relation field, which will act as if the user typed something into the relation field (e.g. automatically filling it out for the user).
This is certainly not the only use of JavaScript; when using JavaScript, though, be warned that the JavaScript code is transmitted to the client and is fully visible to the client.
An Overview of non-Java Technologies
We've discussed some of these technologies earlier in the course, but for the sake of completeness we'll list Java technologies and their comparable non-Java equivalents:
Java | non-Java |
sockets | most languages support socket connections |
RMI | rpc, Corba, .NET remoting |
JDBC | ODBC, embedded SQL, .NET |
Servlets | Perl, CGI, no direct .NET equivalent |
JSP | ASP, PHP, Perl |
Sockets are pretty much the same in any language. The syntax is different, but the same basic idea remains, and a socket opened from one language can talk to a socket opened from another language. The marshalling on the sending end and the unmarshalling on the receiving end must match.
RMI is basically a way of making a method call on an object living on another maching. rpc (remote procedure call) is a way of doing that in C, while Corba allows doing the same thing in many languages. .NET remoting is a similar technology in the .NET suite of tools
JDBC allows for database access. ODBC is the Microsoft technology that JDBC is based on. Embedded SQL is a way of actually putting SQL directly into a program (such as a C or C++ program). .NET provides several ways for accessing databases.
Servlets are a server side program run separately from the web server itself. CGI is the old standard for doing this, using Perl or any other language. There is no direct .NET equivalent to servlets, but there are ways of achieving the same effect.
JSP is a server side scripting technology, directly equivalent to ASP, PHP, or Perl (in those web servers that support embedded Perl, such as Apache).
Since you've already researched the Microsoft technologies for your research paper, we'll focus on looking at non-Java, non-Microsoft technologies today. PHP is a server-side scripting language that has become quite popular. PHP originally stood for "Personal Home Page", since the original developer of PHP designed it because he needed something for creating his own home page. In 1998, the PHP community voted to change the meaning of the name to "PHP: Hypertext Preprocessor".
PHP works in much the same way as JSP: you embed HTML code and PHP code in the same document (this time, with the .php extension), and when the web server is asked for that file it runs the PHP code and the result is an HTML document output to the user.
While JSP was designed to be converted into a servlet, and many of the features reflect that legacy, PHP was designed from the start to be executed in place. The web server must contain a module for parsing and interpreting PHP. Currently, the Apache web server seems to provide the best PHP support.
Let's look at a simple PHP file:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
You can see that the tag "<?php" is used to start a block of PHP code, and the "?>" tag is used to end a block of PHP code. Much like our first JSP example, this example simply uses the echo command to output static HTML code.
PHP also makes a number of variables available to you, similar to the request and response variables from JSP. These variables are:
$_SERVER -- an array of values filled in by the web server.
$_ENV -- an array of environment variables from the machine the web server is running on
$_COOKIE -- an array of cookie values set and read by the web server
$_GET -- an array of variables sent by the web browser using the GET method
$_POST -- an array of variables sent by the web browser using the POST method
$_REQUEST -- an array containing all the values from $_COOKIE, $_GET, and $_POST
$_FILES -- an array containing information on any files that were uploaded with this request
$_SESSION -- an array containing variables for the current session, if one was created
$_GLOBALS -- an array containing all global variables
As you can see, there was a different design between the JSP and PHP variables. JSP seems to provide variables that reflected the interaction with the web server (request and response), while PHP seems to provide variables based on the type of data they contain.
In PHP, variables do not need to be declared, you simply use them. Also, one of the hardest "features" of PHP for most programmers to get used to is the fact that there are no variable types. A given variable has a type based on what it contains, and that can change. For example:
<?php
$a_var=15;
$a_var="Hello";?>
is perfectly legal PHP code. The variable $a_var first contains the integer 15, and then the string "Hello".
Note that a common cause of bugs in PHP code is caused by subtle misspellings in the variable name. Since variables do not need to be declared, a misspelling gets you an entirely new variable. For example:
<?php
$a_var=15;
if ($A_var==0)
echo "zero";?>
This will display "zero", because the variable being set to 15 and the variable being compared to 0 are different (the first character is capitalized in one and not in the other). You will not get a compile error, because you don't need to declare variables first.
Functions can be declared anywhere in the PHP file, and used anywhere (even prior to being declared). For example:
<?php
display ("Before");
function display ($var)
{
echo "Output: $var";
}display ("After");
display (15);
?>
Note that you put a function inside an if statement, which means the function is only available if the if conditional evaluates to true:
<?php
$flag=10;
if ($flag==10)
{
function display ($var)
{
echo "Output: $var";
}
}display ("After");
display (15);
?>
This page would run find as long as $flag was 10. If $flag was not 10, then the display function would never be declared, and the subsequent calls to it would generate a compile error.
Just as in JSP, you can include other PHP files, using the "include" statement. These includes are always run-time includes. Including other PHP files is the way in which you can use common code, such as function libraries or classes, in multiple web pages. For example:
<?php
include "lib/a_class.php";
?>
The entire contents of the file a_class.php is pasted into the current PHP file, and is then available for use.
We saw with servlets how there were template engines that allowed you to separate the HTML code from the program logic. Similar template engines exist for PHP. Here's an example of code that uses such a template engine:
<?php
include "/srv/www/smarty/php/Smarty.class.php";
/* Create an object of the class Smarty */
$smarty = new Smarty;/* Configure some directory locations */
$smarty->template_dir = '/srv/www/smarty/unq/templates/';
$smarty->compile_dir = '/srv/www/smarty/unq/templates_c/';
$smarty->config_dir = '/srv/www/smarty/unq/configs/';
$smarty->cache_dir = '/srv/www/smarty/unq/cache/';/* Get information from session and form */
$user_key = $_SESSION["user_key"];
$area_key = $_REQUEST["area_key"];/* Assign values into template, including arrays */
$smarty->assign ("area_key", $area_key);
if ($user_key != "default")
$smarty->assign ("entities", array ("event", "mobile", "object", "room"));
else
$smarty->assign ("entities", array ("command", "proc", "key_value"));/* Display the template */
$smarty->display("edit_area_form.tpl");?>
If you compare this to the servlet template engine, you'll find that it follows the same pattern. We assign variables into the template object, and then ask the template object to display a particular template file. The template file will contain HTML and scripting code mixed, such as:
{* Smarty *}
{include file="header.tpl" title="Edit $area_name"}
<p>This area can have the following types of entities.
<ul>
{foreach from=$entities item=entity}
<li><a href="unq_entities.php?entity_type={$entity}">{$entity|capitalize}</a></li>
{/foreach}
</ul>
<p>The following links can be used to perform operations on the entire area:
<ul>
<li><a href="unq_delete_area.php?area_key={$area_key}">Delete This Area</a></li>
<li><a href="unq_export_area.php?area_key={$area_key}">Export To UNQ</a></li>
</ul>
{include file="footer.tpl}
Which again, is quite similar to the template scripting for servlets. Using a template engine is needed when working on any decent sized application in either servlets or PHP.
The template engine used for these examples is called Smarty, but other template engines exist for PHP as well.
We will review the topics and types of questions that will appear on the final exam.
Next week is the final exam!