Skip to content

8. Appendices

8.1. Web Development Tools

Here we indicate where to find and how to install the tools necessary for web development. Some tools have seen their versions evolve, and it is possible that the explanations provided here no longer apply to the most recent versions. The reader will then need to adapt... In the web programming course, we will primarily use the following tools, all available for free:

  • a recent browser capable of displaying XML. The examples in the course have been tested with Internet Explorer 6.
  • A recent JDK (Java Development Kit). The examples in this course have been tested with JDK 1.4. This JDK includes the Java 1.4 browser plug-in, which allows browsers to display Java applets using JDK 1.4.
  • A Java development environment for writing Java servlets. Here, it is JBuilder 7.
  • Web servers: Apache, PWS (Personal Web Server), Tomcat.
    • Apache will be used for developing web applications in PERL (Practical Extracting and Reporting Language) or PHP (Personal Home Page)
    • PWS will be used for developing web applications in ASP (Active Server Pages) or PHP
    • Tomcat will be used for developing web applications using Java servlets or JSP (Java Server Pages)
  • A database management application: MySQL
  • EasyPHP: a tool that bundles the Apache web server, the PHP language, and the MySQL DBMS

8.1.1. Web Servers, Browsers, Scripting Languages

  1. Major Web Servers
    • Apache (Linux, Windows)
    • Internet Information Server (IIS) (NT), Personal Web Server (PWS) (Windows 9x)
  1. Major browsers
    • Internet Explorer (Windows)
    • Netscape (Linux, Windows)
  1. Server-side scripting languages
    • VBScript (IIS, PWS)
    • JavaScript (IIS, PWS)
    • Perl (Apache, IIS, PWS)
    • PHP (Apache, IIS, PWS)
    • Java (Apache, Tomcat)
    • .NET languages
  1. Client-side scripting languages
    • VBScript (IE)
    • JavaScript (IE, Netscape)
    • PerlScript (IE)
    • Java (IE, Netscape)

8.1.2. Where to find the tools

Netscape
http://www.netscape.com/ (downloads link)
Internet Explorer
http://www.microsoft.com/windows/ie/default.asp
PHP
http://www.php.net
http://www.php.net/downloads.php (Windows Binaries)
PERL
http://www.activestate.com http://www.activestate.com/Products/
http://www.activestate.com/Products/ActivePerl/
VBScript, JavaScript
http://msdn.microsoft.com/scripting (follow the Windows Script link)
JAVA
http://java.sun.com/
http://java.sun.com/downloads.html (JSE)
http://java.sun.com/j2se/1.4/download.html
Apache
http://www.apache.org/
http://www.apache.org/dist/httpd/binaries/win32/
PWS
included in the NT 4.0 Option Pack for Windows 95
included on the Windows 98 CD
http://www.microsoft.com/ntserver/nts/downloads/recommended/NT4OptPk/win95.asp
IIS
(Windows NT/2000)
http://www.microsoft.com
Tomcat
http://jakarta.apache.org/tomcat/
JBuilder
http://www.borland.com/jbuilder/
http://www.borland.com/products/downloads/download_jbuilder.html
EasyPHP
http://www.easyphp.org/
http://www.easyphp.org/telechargements.php3

8.1.3. EasyPHP

This application is very convenient because it includes the following in a single package:

  • the Apache Web server (1.3.x)
  • the PHP language (4.x)
  • the MySQL DBMS (3.23.x)
  • a MySQL administration tool: PhpMyAdmin

The installation application looks like this:

Image

Installing EasyPHP is straightforward, and a directory structure is created in the file system:

Image

easyphp.exe
the application executable
apache
the Apache server directory structure
mysql
the MySQL database directory
phpMyAdmin
the phpMyAdmin application directory structure
PHP
the PHP directory structure
www
root of the directory tree for web pages served by the EasyPHP Apache server
cgi-bin
directory where you can place CGI scripts for the Apache server

The main advantage of EasyPHP is that the application comes preconfigured. Thus, Apache, PHP, and MySQL are already configured to work together. When you launch EasyPHP via its shortcut in the Programs menu, an icon appears in the bottom-right corner of the screen.

This is the letter E with a red dot, which should flash if the Apache web server and the MySQL database are operational. When you right-click on it, you access the menu options:

Image

The Administration option allows you to configure settings and run functionality tests:

Image

8.1.3.1. PHP Administration

The PHP Info button should allow you to verify that the Apache-PHP combination is working properly: a PHP information page should appear:

Image

The Extensions button displays a list of installed PHP extensions. These are actually function libraries.

Image

The screen above shows, for example, that the functions required to use the MySQL database are present.

The Settings button displays the username and password for the MySQL database administrator.

Image

Using the MySQL database is beyond the scope of this quick overview, but it is clear here that a password should be set for the database administrator.

8.1.3.2. Apache Administration

Still on the EasyPHP administration page, the "Your Aliases" link allows you to define aliases associated with a directory. This lets you place web pages outside the www directory in the EasyPHP directory structure.

Image

If you enter the following information on the page above:

Image

and click the "Validate" button, the following lines are added to the <easyphp>\apache\conf\httpd.conf file:

    Alias /st/ "e:/data/serge/web/"

    <Directory "e:/data/serge/web">
        Options FollowSymLinks Indexes
        AllowOverride None
        Order deny,allow
        allow from 127.0.0.1
        deny from all
    </Directory>

<easyphp> refers to the EasyPHP installation directory. httpd.conf is the Apache server configuration file. You can therefore achieve the same result by editing this file directly. Changes to the httpd.conf file are normally applied immediately by Apache. If this is not the case, you will need to stop and restart the server, using the EasyPHP icon:

Image

To finish our example, we can now place web pages in the directory tree e:\data\serge\web:

C:\winnt\system32>dir e:\data\serge\web\html\balises.htm

14/07/2002  17:02                3 767 balises.htm

and request this page using the alias st:

Image

In this example, the Apache server has been configured to run on port 81. Its default port is 80. This is controlled by the following line in the httpd.conf file we’ve already seen:

Port 81

8.1.3.3. The Apache configuration file htpd.conf

When you want to fine-tune Apache, you have to manually edit its httpd.conf configuration file, located here in the <easyphp>\apache\conf folder:

Image

Here are a few key points to note in this configuration file:

line(s)
role
ServerRoot "D:/Program Files/Apache Group/Apache"
specifies the folder containing the Apache directory tree
Port 80
specifies which port the web server will use. Typically, this is 80. By changing this line, you can have the web server run on a different port
ServerAdmin root@istia.univ-angers.fr
the email address of the Apache server administrator
ServerName stahe.istia.uang
the name of the machine on which the Apache server is running
ServerRoot "E:/Program Files/EasyPHP/apache"
the installation directory of the Apache server. When relative file names appear in the configuration file, they are relative to this directory.
DocumentRoot "E:/Program Files/EasyPHP/www"
the root directory of the tree of web pages served by the server. Here, the URL http://machine/rep1/fic1.html will correspond to the file E:\Program Files\EasyPHP\www\rep1\fic1.html
<Directory "E:/Program Files/EasyPHP/www">
sets the properties of the previous folder
ErrorLog logs/error.log
logs folder, so effectively <ServerRoot>\logs\error.log: E:\Program Files\EasyPHP\apache\logs\error.log. This is the file to check if you find that the Apache server is not working.
    ScriptAlias /cgi-bin/ "E:/Program Files/EasyPHP/cgi-bin/"
E:\Program Files\EasyPHP\cgi-bin will be the root of the directory tree where you can place CGI scripts. Thus, the URL http://machine/cgi-bin/rep1/script1.pl will be the URL for the CGI script E:\Program Files\EasyPHP\cgi-bin\rep1\script1.pl.
<Directory "E:/Program Files/EasyPHP/cgi-bin/">
sets the properties of the folder above
LoadModule php4_module "E:/Program
 Files/EasyPHP/php/php4apache.dll"
AddModule mod_php4.c
Lines for loading modules that allow Apache to work with PHP4.
    AddType application/x-httpd-php
 .phtml .pwml .php3
.php4 .php .php2 .inc
sets the file extensions to be treated as PHP files

8.1.3.4. MySQL Administration with PhpMyAdmin

On the EasyPHP administration page, click the PhpMyAdmin button:

Image

The drop-down list under Home allows you to view the
.
The number in parentheses is the number of tables. If you
select a database, its tables are displayed:

The web page offers a number of operations on the database:

Image

If you click the View user link:

Image

There is only one user here: root, who is the MySQL administrator. By following the Edit link, you could change their password, which is currently blank—a practice not recommended for an administrator.

We won’t say any more about phpMyAdmin, which is a feature-rich program that would merit a discussion spanning several pages.

8.1.4. PHP

We have seen how to obtain PHP through the EasyPHP application. To obtain PHP directly, go to the website http://www.php.net.

PHP isn’t limited to web use. You can use it as a scripting language on Windows. Create the following script and save it as date.php:

<?
  // script php affichant l'heure
  $maintenant=date("j/m/y, H:i:s",time());
  echo "Nous sommes le $maintenant";
?>

In a DOS window, navigate to the directory containing date.php and run it:

E:\data\serge\php\essais>"e:\program files\easyphp\php\php.exe" date.php
X-Powered-By: PHP/4.2.0
Content-type: text/html

Nous sommes le 18/07/02, 09:31:01

8.1.5. PERL

It is best if Internet Explorer is already installed. If it is present, Active Perl will configure it to accept PERL scripts in HTML pages, scripts that will be executed by IE itself on the client side. The Active Perl website is at the URL http://www.activestate.comA installation, PERL will be installed in a directory we will call <perl>. It contains the following directory structure:

DEISL1   ISU        32 403  23/06/00  17:16 DeIsL1.isu
BIN            <REP>        23/06/00  17:15 bin
LIB            <REP>        23/06/00  17:15 lib
HTML           <REP>        23/06/00  17:15 html
EG             <REP>        23/06/00  17:15 eg
SITE           <REP>        23/06/00  17:15 site
HTMLHELP       <REP>        28/06/00  18:37 htmlhelp

The perl.exe executable is located in <perl>\bin. Perl is a scripting language that runs on Windows and Unix. It is also used in web programming. Let’s write our first script:

# script PERL affichant l'heure

# modules
use strict;

# programme
my ($secondes,$minutes,$heure)=localtime(time);
print "Il est $heure:$minutes:$secondes\n";

Save this script to a file named heure.pl. Open a DOS window, navigate to the directory containing the script, and run it:

E:\data\serge\Perl\Essais>e:\perl\bin\perl.exe heure.pl
Il est 9:34:21

8.1.6. VBScript, JavaScript, PerlScript

These are scripting languages for Windows. They can run in various environments such as

  • Windows Scripting Host for direct use in Windows, particularly for writing system administration scripts
  • Internet Explorer. It is then used within HTML pages, to which it adds a level of interactivity that cannot be achieved with HTML alone.
  • Internet Information Server (IIS), Microsoft’s web server on NT/2000, and its equivalent, Personal Web Server (PWS), on Win9x. In this case, VBScript is used for server-side web programming, a technology called ASP (Active Server Pages) by Microsoft.

Download the installation file from the URL: http://msdn.microsoft.com/scripting and follow the Windows Script links. The following are installed:

  • the Windows Scripting Host container, which supports various scripting languages such as VBScript and JavaScript, as well as others like PerlScript, which is included with Active Perl.
  • a VBScript interpreter
  • a JavaScript interpreter

Let’s run a few quick tests. Let’s build the following VBScript program:

' a class
class personne
  Dim nom
  Dim age
End class

' creation of a person object
Set p1=new personne
With p1
  .nom="dupont"
  .age=18
End With

' display properties person p1
With p1
  wscript.echo "nom=" & .nom
  wscript.echo "age=" & .age
End With

This program uses objects. Let's call it objects.vbs (the .vbs extension indicates a VBScript file). Navigate to the directory where it is located and run it:

E:\data\serge\windowsScripting\vbscript\poly\objets>cscript objets.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

nom=dupont
age=18

Now let's build the following JavaScript program that uses arrays:

// tableau dans un variant
// tableau vide
tableau=new Array();
affiche(tableau);
// tableau croît dynamiquement
for(i=0;i<3;i++){
  tableau.push(i*10);
}
// affichage tableau
affiche(tableau);
// encore
for(i=3;i<6;i++){
  tableau.push(i*10);
}
affiche(tableau);

// tableaux à plusieurs dimensions
WScript.echo("-----------------------------");

tableau2=new Array();
for(i=0;i<3;i++){
  tableau2.push(new Array());
  for(j=0;j<4;j++){
    tableau2[i].push(i*10+j);
  }//for j
}// for i
affiche2(tableau2);

// fin
WScript.quit(0);

// ---------------------------------------------------------
function affiche(tableau){
  // affichage tableau
  for(i=0;i<tableau.length;i++){
    WScript.echo("tableau[" + i + "]=" + tableau[i]);
  }//for
}//function

// ---------------------------------------------------------
function affiche2(tableau){
  // affichage tableau
  for(i=0;i<tableau.length;i++){
    for(j=0;j<tableau[i].length;j++){
      WScript.echo("tableau[" + i + "," + j + "]=" + tableau[i][j]);
    }// for j
  }//for i
}//function

This program uses arrays. Let's call it arrays.js (the .js suffix indicates a JavaScript file). Navigate to the directory where it is located and run it:

E:\data\serge\windowsScripting\javascript\poly\tableaux>cscript tableaux.js
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. Tous droits réservés.

tableau[0]=0
tableau[1]=10
tableau[2]=20
tableau[0]=0
tableau[1]=10
tableau[2]=20
tableau[3]=30
tableau[4]=40
tableau[5]=50
-----------------------------
tableau[0,0]=0
tableau[0,1]=1
tableau[0,2]=2
tableau[0,3]=3
tableau[1,0]=10
tableau[1,1]=11
tableau[1,2]=12
tableau[1,3]=13
tableau[2,0]=20
tableau[2,1]=21
tableau[2,2]=22
tableau[2,3]=23

One last example in PerlScript to finish up. You must have Active Perl installed to access PerlScript.

<job id="PERL1">
  <script language="PerlScript">
      # du Perl classique
    %dico=("maurice"=>"juliette","philippe"=>"marianne");
    @cles= keys %dico;
    for ($i=0;$i<=$#cles;$i++){
        $cle=$cles[$i];
      $valeur=$dico{$cle};
        $WScript->echo ("clé=".$cle.", valeur=".$valeur);
    }
    # du perlscript utilisant les objets Windows Script
    $dico=$WScript->CreateObject("Scripting.Dictionary");
    $dico->add("maurice","juliette");
    $dico->add("philippe","marianne");
    $WScript->echo($dico->item("maurice"));
    $WScript->echo($dico->item("philippe"));    
  </script>
</job>

This program demonstrates the creation and use of two dictionaries: one in the classic Perl style, the other using the Windows Script Scripting Dictionary object. Let’s save this code to the file dico.wsf (wsf is the file extension for Windows Script files). Navigate to the program’s folder and run it:

E:\data\serge\windowsScripting\perlscript\essais>cscript dico.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. Tous droits réservés.

clé=philippe, valeur=marianne
clé=maurice, valeur=juliette
juliette
marianne

PerlScript can use objects from the container in which it runs. Here, these were objects from the Windows Script container. In the context of web programming, VBScript, JavaScript, and PerlScript scripts can be executed either within the IE browser or on a PWS or IIS server. If the script is somewhat complex, it may be wise to test it outside the web context, within the Windows Script container as seen previously. This way, you can only test the script’s functions that do not use browser- or server-specific objects. Even with this restriction, this option remains useful because it is generally quite impractical to debug scripts running within web servers or browsers.

8.1.7. JAVA

Java is available at the URL: http://www.sun.com and is installed in a directory structure called <java> that contains the following elements:

22/05/2002  05:51       <DIR>          .
22/05/2002  05:51       <DIR>          ..
22/05/2002  05:51       <DIR>          bin
22/05/2002  05:51       <DIR>          jre
07/02/2002  12:52                8 277 README.txt
07/02/2002  12:52               13 853 LICENSE
07/02/2002  12:52                4 516 COPYRIGHT
07/02/2002  12:52               15 290 readme.html
22/05/2002  05:51       <DIR>          lib
22/05/2002  05:51       <DIR>          include
22/05/2002  05:51       <DIR>          demo
07/02/2002  12:52           10 377 848 src.zip
11/02/2002  12:55       <DIR>          docs

In the bin directory, you will find javac.exe, the Java compiler, and java.exe, the Java Virtual Machine. You can perform the following tests:

  1. Write the following script:
//java program displaying the time

import java.io.*;
import java.util.*;

public class heure{
  public static void main(String arg[]){
     // retrieve date & time
     Date maintenant=new Date();
      // we display
     System.out.println("Il est "+maintenant.getHours()+
        ":"+maintenant.getMinutes()+":"+maintenant.getSeconds());
  }//hand
}//class
  1. Save this program as heure.java. Open a DOS window. Navigate to the directory containing the heure.java file and compile it:
D:\data\java\essais>c:\jdk1.3\bin\javac heure.java
Note: heure.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

In the command above, c:\jdk1.3\bin\javac must be replaced with the exact path to the javac.exe compiler. You should now have a file named heure.class in the same directory as heure.java; this is the program that will now be executed by the java.exe virtual machine.

  1. Run the program:
D:\data\java\essais>c:\jdk1.3\bin\java heure
Il est 10:44:2

8.1.8. Apache Server

We have seen that the Apache server can be obtained through the EasyPHP application. To download it directly, go to the Apache website: http://www.apache.org. The installation creates a directory structure containing all the files necessary for the server. Let’s call this directory <apache>. It contains a directory structure similar to the following:

UNINST   ISU       118 805  23/06/00  17:09 Uninst.isu
HTDOCS         <REP>        23/06/00  17:09 htdocs
APACHE~1 DLL       299 008  25/02/00  21:11 ApacheCore.dll
ANNOUN~1             3 000  23/02/00  16:51 Announcement
ABOUT_~1            13 197  31/03/99  18:42 ABOUT_APACHE
APACHE   EXE        20 480  25/02/00  21:04 Apache.exe
KEYS                36 437  20/08/99  11:57 KEYS
LICENSE              2 907  01/01/99  13:04 LICENSE
MAKEFI~1 TMP        27 370  11/01/00  13:47 Makefile.tmpl
README               2 109  01/04/98   6:59 README
README   NT          3 223  19/03/99   9:55 README.NT
WARNIN~1 TXT           339  21/09/98  13:09 WARNING-NT.TXT
BIN            <REP>        23/06/00  17:09 bin
MODULES        <REP>        23/06/00  17:09 modules
ICONS          <REP>        23/06/00  17:09 icons
LOGS           <REP>        23/06/00  17:09 logs
CONF           <REP>        23/06/00  17:09 conf
CGI-BIN        <REP>        23/06/00  17:09 cgi-bin
PROXY          <REP>        23/06/00  17:09 proxy
INSTALL  LOG         3 779  23/06/00  17:09 install.log
conf
Apache configuration files directory
logs
Apache log files (monitoring) directory
bin
Apache executables

8.1.8.1. Configuration

In the <Apache>\conf directory, you will find the following files: httpd.conf, srm.conf, access.conf. In the latest versions of Apache, these three files have been combined into httpd.conf. We have already covered the key points of this configuration file. In the following examples, the Apache version of EasyPHP was used for testing, and therefore its configuration file. In this file, DocumentRoot, which designates the root of the web page directory tree, is e:\program files\easyphp\www.

To test, create the file intro.php with the following single line:

<? phpinfo() ?>

and place it at the root of the Apache server’s web pages (DocumentRoot above). Request the URL http://localhost/intro.php. You should see a list of PHP information:

Image

The following PHP script displays the time. We’ve seen it before:

<?php
   // time: number of milliseconds since 01/01/1970
   // "date-time display format
   // d: 2-digit day
   // m: 2-digit month
   // y: 2-digit year
   // H: hour 0.23
   // i : minutes
   // s: seconds
  print "Nous sommes le " . date("d/m/y H:i:s",time());
?>

Place this text file in the root directory of the Apache server (DocumentRoot) and name it date.php. Open a browser and enter the URL http://localhost/date.php. You will see the following page:

Image

This is achieved using a line of the form: ScriptAlias /cgi-bin/ "E:/Program Files/EasyPHP/cgi-bin/" in the file <apache>\conf\httpd.conf. Its syntax is ScriptAlias /cgi-bin/ "<cgi-bin>" where <cgi-bin> is the folder where CGI scripts can be placed. CGI (Common Gateway Interface) is a standard for communication between the web server and applications. A client requests a dynamic page from the web server, i.e., a page generated by a program. The web server must therefore instruct a program to generate the page. CGI defines the interaction between the server and the program, specifically how information is transmitted between these two entities.

If necessary, modify the line ScriptAlias /cgi-bin/ &quot;&lt;cgi-bin&gt;&quot; and restart the Apache server. Then perform the following test:

  1. Write the script:
#!c:\perl\bin\perl.exe

# script PERL affichant l'heure

# modules
use strict;

# programme
my ($secondes,$minutes,$heure)=localtime(time);
print <<FINHTML
Content-Type: text/html

<html>
 <head>
  <title>heure</title>
 </head>
 <body>
  <h1>Il est $heure:$minutes:$secondes</h1>
 </body>
FINHTML
;
  1. Place this script in <cgi-bin>\heure.pl, where <cgi-bin> is the directory that can receive CGI scripts (see httpd.conf). The first line, #!c:\perl\bin\perl.exe, specifies the path to the perl.exe executable. Modify it if necessary.
  2. Start Apache if you haven't already
  3. Request the URL http://localhost/cgi-bin/heure.pl in a browser. You will see the following page:

Image

8.1.9. The PWS server

8.1.9.1. Installation

The PWS (Personal Web Server) is a personal version of Microsoft's IIS (Internet Information Server). IIS is available on NT and 2000 machines. On Win9x machines, PWS is normally included in the Internet Explorer installation package. However, it is not installed by default. You must perform a custom installation of IE and request the installation of PWS. It is also available in the NT 4.0 Option Pack for Windows 95.

8.1.9.2. Initial Tests

The root directory for PWS web pages is drive:\inetpub\wwwroot, where drive is the disk on which you installed PWS. We will assume hereafter that this drive is D. Thus, the URL http://machine/rep1/page1.html corresponds to the file d:\inetpub\wwwroot\rep1\page1.html. The PWS server interprets any file with the .asp (Active Server Pages) extension as a script that it must execute to generate an HTML page.

PWS runs on port 80 by default. The Apache web server does too... You must therefore stop Apache to work with PWS if you have both servers. The other solution is to configure Apache to run on a different port. In the Apache configuration file httpd.conf, replace the line Port 80</mark> with Port 81</mark>. Apache will now run on port 81 and can be used simultaneously with PWS. If PWS is running and you request the URL http://localhost, you will see a page similar to the following:

Image

  1. Below is a .reg file for modifying the registry. Double-click this file to modify the registry. Here, the required DLL is located in d:\php4 along with the PHP executable. Modify as needed. The backslashes (\) must be doubled in the DLL path.
REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script Map]
".php"="d:\\php4\\php4isapi.dll"
  1. Restart the machine so that the registry change takes effect.
  1. Create a "php" folder in d:\inetpub\wwwroot, which is the root of the PWS server. Once done, open PWS and go to the "Advanced" tab. Click the "Add" button to create a virtual directory:
Répertoire/Parcourir : d:\inetpub\wwwroot\php
Alias : php
Cocher la case exécuter.
  1. Confirm the settings and restart PWS. Place the file intro.php in d:\inetpub\wwwroot\php, containing only the following line:
<? phpinfo() ?>
  1. Request the URL http://localhost/php/intro.php from the PWS server. You should see the list of PHP information already displayed with Apache.

8.1.10. Tomcat: Java servlets and JSP (Java Server Pages)

Tomcat is a web server that generates HTML pages using servlets (Java programs executed by the web server) or JSP (Java Server Pages), which combine Java code with HTML code. It is the equivalent of ASP (Active Server Pages) on Microsoft’s IIS/PWS server, where VBScript or JavaScript code is combined with HTML code.

8.1.10.1. Installation

Tomcat is available at the URL: http://jakarta.apache.org. You will download an .exe installation file. When you run this program, it first prompts you to select which JDK it will use. This is because Tomcat requires a JDK to install itself and subsequently to compile and execute Java servlets. You must therefore have a Java JDK installed before installing Tomcat. The most recent JDK is recommended. The installation will create a <tomcat> directory structure:

Image

which simply involves extracting this archive into a directory. Choose a directory whose path contains only names without spaces (not, for example, "Program Files"), because there is a bug in the Tomcat installation process. Use, for example, C:\tomcat or D:\tomcat. Let’s call this directory <tomcat>. Inside it, you will find a folder named jakarta-tomcat, and within that folder, the following directory structure:

LOGS           <REP>        15/11/00   9:04 logs
LICENSE              2 876  18/04/00  15:56 LICENSE
CONF           <REP>        15/11/00   8:53 conf
DOC            <REP>        15/11/00   8:53 doc
LIB            <REP>        15/11/00   8:53 lib
SRC            <REP>        15/11/00   8:53 src
WEBAPPS        <REP>        15/11/00   8:53 webapps
BIN            <REP>        15/11/00   8:53 bin
WORK           <REP>        15/11/00   9:04 work

8.1.10.2. Starting/Stopping the Tomcat Web Server

Tomcat is a web server, just like Apache or PWS. To launch it, use the links in the Programs menu:

Start Tomcat
to start Tomcat
Stop Tomcat
to stop it

When you start Tomcat, a DOS window appears with the following content:

Image

You can minimize this DOS window. It will remain open as long as Tomcat is running. You can then proceed to the first tests. The Tomcat web server runs on port 8080. Once Tomcat is running, open a web browser and enter the URL http://localhost:8080. You should see the following page:

Image

Follow the Servlet Examples link:

Image

Click the Execute link for RequestParameters, then the one for Source. You’ll get a first glimpse of what a Java servlet is. You can do the same with the links on the JSP pages.

To stop Tomcat, use the Stop Tomcat link in the Programs menu.

8.1.11. JBuilder

JBuilder is a Java application development environment. To build Java servlets without graphical interfaces, such an environment is not strictly necessary. A text editor and a JDK will suffice. However, JBuilder offers a few advantages over the previous method:

  • ease of debugging: the compiler highlights erroneous lines in a program, and it is easy to navigate to them
  • code completion: when using a Java object, JBuilder displays a list of its properties and methods inline. This is very useful, given that most Java objects have numerous properties and methods that are difficult to remember.

JBuilder can be found at http://www.borland.com/jbuilder. You must fill out a form to obtain the software. An activation key is sent via email. To install JBuilder 7, for example, the following steps were taken:

  • Three ZIP files were downloaded: one for the application, one for the documentation, and one for the examples. Each of these ZIP files has a separate link on the JBuilder website.
  • First, the application was installed, then the documentation, and finally the examples
  • When you launch the application for the first time, an activation key is requested: this is the one that was sent to you via email. In version 7, this key is actually an entire text file that can be placed, for example, in the JB7 installation folder. When the key is requested, you then specify the file in question. Once this is done, the key will not be requested again.

There are a few useful configurations to set up if you want to use JBuilder to build Java servlets. The so-called "Personal" version of JBuilder is a stripped-down version that does not include all the classes necessary for Java web development. You can configure JBuilder to use the class libraries provided by Tomcat. Here’s how:

  • Launch JBuilder

Image

  • enable the Tools/Configure JDKs option

Image

In the JDK Settings section above, the Name field typically displays "JDK 1.3.1." If you have a newer JDK, use the Change button to specify its installation directory. In the example above, we have specified the directory E:\Program Files\jdk14, where a JDK 1.4 was installed. From now on, JBuilder will use this JDK for its compilations and executions. In the (Class, Source, Documentation) section, you’ll see a list of all the class libraries that JBuilder will scan—in this case, the classes from JDK 1.4. The classes included in this JDK are not sufficient for Java web development. To add other class libraries, use the Add button and specify the additional .jar files you want to use. .jar files are class libraries. Tomcat 4.x includes all the class libraries necessary for web development. They are located in <tomcat>\common\lib, where <tomcat> is the Tomcat installation directory:

Image

Using the Add button, we will add these libraries, one by one, to the list of libraries scanned by JBuilder:

Image

From now on, you can compile Java programs compliant with the J2EE standard, including Java servlets. JBuilder is used only for compilation; execution is subsequently handled by Tomcat according to the procedures explained in the course.

8.2. Program source code

8.2.1. The generic TCP client

Many services created at the dawn of the Internet operate according to the echo server model studied earlier: client-server communication occurs via the exchange of text lines. We will write a generic TCP client that will be launched as follows: java cltTCPgenerique server port

This TCP client will connect to port port on server server. Once connected, it will create two threads:

  1. a thread responsible for reading commands typed on the keyboard and sending them to the server
  2. a thread responsible for reading the server’s responses and displaying them on the screen

Why two threads? Each TCP-IP service has its own specific protocol, and the following situations sometimes arise:

  • the client must send several lines of text before receiving a response
  • a server’s response may contain multiple lines of text

Therefore, the loop that sends a single line to the server and receives a single line from the server is not always suitable. We will therefore create two separate loops:

  • a loop to read commands typed on the keyboard to be sent to the server. The user will signal the end of commands with the keyword "fin".
  • a loop to receive and display the server’s responses. This will be an infinite loop that will only be interrupted by the server closing the network connection or by the user typing the “end” command at the keyboard.

To have these two separate loops, we need two independent threads. Let’s look at an example of execution where our generic TCP client connects to an SMTP (Simple Mail Transfer Protocol) service. This service is responsible for routing email to its recipients. It operates on port 25 and uses a text-based exchange protocol.

Dos>java clientTCPgenerique istia.univ-angers.fr 25
Commandes :
<-- 220 istia.univ-angers.fr ESMTP Sendmail 8.11.6/8.9.3; Mon, 13 May 2002 08:37:26 +0200
help
<-- 502 5.3.0 Sendmail 8.11.6 -- HELP not implemented
mail from: machin@univ-angers.fr
<-- 250 2.1.0 machin@univ-angers.fr... Sender ok
rcpt to: serge.tahe@istia.univ-angers.fr
<-- 250 2.1.5 serge.tahe@istia.univ-angers.fr... Recipient ok
data
<-- 354 Enter mail, end with "." on a line by itself
Subject: test

ligne1
ligne2
ligne3
.
<-- 250 2.0.0 g4D6bks25951 Message accepted for delivery
quit
<-- 221 2.0.0 istia.univ-angers.fr closing connection
[fin du thread de lecture des réponses du serveur]
fin
[fin du thread d'envoi des commandes au serveur]

Let's comment on these client-server exchanges:

  • The SMTP service sends a welcome message when a client connects to it:
<-- 220 istia.univ-angers.fr ESMTP Sendmail 8.11.6/8.9.3; Mon, 13 May 2002 08:37:26 +0200
  • Some services have a "help" command that provides information on the commands available for that service. That is not the case here. The SMTP commands used in the example are as follows:
    • mail from: sender, to specify the sender’s email address
    • rcpt to: recipient, to specify the email address of the message’s recipient. If there are multiple recipients, the rcpt to: command is repeated as many times as necessary for each recipient.
    • data, which signals to the SMTP server that the message is about to be sent. As indicated in the server’s response, this is a sequence of lines ending with a line containing only a period. A message may have headers separated from the message body by a blank line. In our example, we included a subject using the Subject: keyword
  • Once the message is sent, we can tell the server that we are done using the quit command. The server then closes the network connection. The read thread can detect this event and stop.
  • The user then types "end" on the keyboard to also stop the thread reading the commands typed on the keyboard.

If we check the received email, we see the following (Outlook):

Image

Note that the SMTP service cannot detect whether a sender is valid or not. Therefore, you can never trust the "From" field of a message. In this case, the sender machin@univ-angers.fr did not exist.

This generic TCP client allows us to explore the communication protocol for Internet services and, from there, build specialized classes for clients of those services. Let’s explore the communication protocol for the POP (Post Office Protocol) service, which allows users to retrieve their emails stored on a server. It operates on port 110.

Dos> java clientTCPgenerique istia.univ-angers.fr 110
Commandes :
<-- +OK Qpopper (version 4.0.3) at istia.univ-angers.fr starting.
help
<-- -ERR Unknown command: "help".
user st
<-- +OK Password required for st.
pass monpassword
<-- +OK st has 157 visible messages (0 hidden) in 11755927 octets.
list
<-- +OK 157 visible messages (11755927 octets)
<-- 1 892847
<-- 2 171661
...
<-- 156 2843
<-- 157 2796
<-- .
retr 157
<-- +OK 2796 octets
<-- Received: from lagaffe.univ-angers.fr (lagaffe.univ-angers.fr [193.49.144.1])
<--     by istia.univ-angers.fr (8.11.6/8.9.3) with ESMTP id g4D6wZs26600;
<--     Mon, 13 May 2002 08:58:35 +0200
<-- Received: from jaume ([193.49.146.242])
<--     by lagaffe.univ-angers.fr (8.11.1/8.11.2/GeO20000215) with SMTP id g4D6wSd37691;
<--     Mon, 13 May 2002 08:58:28 +0200 (CEST)
...
<-- ------------------------------------------------------------------------
<-- NOC-RENATER2                  Tl.  : 0800 77 47 95
<-- Fax : (+33) 01 40 78 64 00 ,  Email : noc-r2@cssi.renater.fr
<-- ------------------------------------------------------------------------
<--
<-- .
quit
<-- +OK Pop server at istia.univ-angers.fr signing off.
[fin du thread de lecture des réponses du serveur]
fin
[fin du thread d'envoi des commandes au serveur]

The main commands are as follows:

  • user login, where you enter your login on the machine that hosts your emails
  • pass password, where you enter the password associated with the previous login
  • list, to get a list of messages in the format number, size in bytes
  • retr i, to read message number i
  • quit, to end the session.

Let’s now explore the communication protocol between a client and a web server, which typically runs on port 80:

Dos> java clientTCPgenerique istia.univ-angers.fr 80
Commandes :
GET /index.html HTTP/1.0

<-- HTTP/1.1 200 OK
<-- Date: Mon, 13 May 2002 07:30:58 GMT
<-- Server: Apache/1.3.12 (Unix)  (Red Hat/Linux) PHP/3.0.15 mod_perl/1.21
<-- Last-Modified: Wed, 06 Feb 2002 09:00:58 GMT
<-- ETag: "23432-2bf3-3c60f0ca"
<-- Accept-Ranges: bytes
<-- Content-Length: 11251
<-- Connection: close
<-- Content-Type: text/html
<--
<-- <html>
<--
<-- <head>
<-- <meta http-equiv="Content-Type"
<-- content="text/html; charset=iso-8859-1">
<-- <meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<-- <title>Bienvenue a l'ISTIA - Universite d'Angers</title>
<-- </head>
....
<-- face="Verdana"> - Dernire mise  jour le <b>10 janvier 2002</b></font></p>
<-- </body>
<-- </html>
<--
[fin du thread de lecture des réponses du serveur]
fin
[fin du thread d'envoi des commandes au serveur]

A web client sends its commands to the server according to the following pattern:

commande1
commande2
...
commanden
[ligne vide]

The web server responds only after receiving the empty line. In this example, we used only one command:

GET /index.html HTTP/1.0

which requests the URL /index.html from the server and indicates that it is using HTTP version 1.0. The most recent version of this protocol is 1.1. The example shows that the server responded by sending the contents of the index.html file and then closed the connection, as we can see the response reading thread terminating. Before sending the contents of the index.html file, the web server sent a series of headers followed by an empty line:

<-- HTTP/1.1 200 OK
<-- Date: Mon, 13 May 2002 07:30:58 GMT
<-- Server: Apache/1.3.12 (Unix)  (Red Hat/Linux) PHP/3.0.15 mod_perl/1.21
<-- Last-Modified: Wed, 06 Feb 2002 09:00:58 GMT
<-- ETag: "23432-2bf3-3c60f0ca"
<-- Accept-Ranges: bytes
<-- Content-Length: 11251
<-- Connection: close
<-- Content-Type: text/html
<--
<-- <html>

The line <html> is the first line of the /index.html file. The preceding text is called HTTP (HyperText Transfer Protocol) headers. We won’t go into detail about these headers here, but keep in mind that our generic client provides access to them, which can be helpful for understanding them. For example, the first line:

<-- HTTP/1.1 200 OK

indicates that the contacted web server supports the HTTP/1.1 protocol and that it successfully found the requested file (200 OK), where 200 is an HTTP response code. The lines

<-- Content-Length: 11251
<-- Connection: close
<-- Content-Type: text/html

tell the client that it will receive 11,251 bytes of HTML (HyperText Markup Language) text and that the connection will be closed once the data has been sent.

So here we have a very handy TCP client. It certainly does less than the official telnet program, but it was interesting to write it ourselves. The generic TCP client program is as follows:

// imported packages
import java.io.*;
import java.net.*;

public class clientTCPgenerique{

    // receives the characteristics of a service as a parameter in the form
     // server port
     // connects to the service
     // creates a thread to read keyboard commands
     // these will be sent to the
     // creates a thread to read server responses
     // these will be displayed on the screen
     // the whole thing ends with the command end typed on the keyboard

   // instance variable
  private static Socket client;

    public static void main(String[] args){

         // syntax
        final String syntaxe="pg serveur port";

         // number of arguments
        if(args.length != 2)
            erreur(syntaxe,1);

         // note the server name
        String serveur=args[0];

         // port must be integer >0
        int port=0;
        boolean erreurPort=false;
        Exception E=null;
        try{
            port=Integer.parseInt(args[1]);
        }catch(Exception e){
            E=e;
            erreurPort=true;
        }
        erreurPort=erreurPort || port <=0;
        if(erreurPort)
            erreur(syntaxe+"\n"+"Port incorrect ("+E+")",2);

        client=null;
         // there may be problems
        try{
             // connect to the service
            client=new Socket(serveur,port);
        }catch(Exception ex){
             // error
            erreur("Impossible de se connecter au service ("+ serveur
                +","+port+"), erreur : "+ex.getMessage(),3);
             // end
            return;
        }//catch

         // create read/write threads
    new ClientSend(client).start();
    new ClientReceive(client).start();

         // end thread main
        return;
    }// hand

     // error display
    public static void erreur(String msg, int exitCode){
         // error display
        System.err.println(msg);
         // stop with error
        System.exit(exitCode);
    }//error
}//class  

class ClientSend extends Thread {
    // class for reading keyboard commands
     // and send them to a server via a tcp client passed in parameter

    private Socket client;    // tcp client

     // manufacturer
    public ClientSend(Socket client){
         // we note the tcp client
        this.client=client;
    }//manufacturer

     // thread Run method
    public void run(){

        // local data
        PrintWriter OUT=null;            // network write streams
    BufferedReader IN=null;        // keyboard flow
        String commande=null;            // command read from keyboard

         // error management
        try{
             // network write stream creation
            OUT=new PrintWriter(client.getOutputStream(),true);
      // keyboard input stream creation
      IN=new BufferedReader(new InputStreamReader(System.in));
            // order entry-send loop
            System.out.println("Commandes : ");
            while(true){
                 // read command typed on keyboard
                commande=IN.readLine().trim();
                 // finished?
                if (commande.toLowerCase().equals("fin")) break;
                // send order to server
                OUT.println(commande);
                 // next order
            }//while
        }catch(Exception ex){
             // error
            System.err.println("Envoi : L'erreur suivante s'est produite : " + ex.getMessage());
        }//catch
         // end - close flows
        try{
            OUT.close();client.close();
        }catch(Exception ex){}
         // signals the end of the thread
        System.out.println("[Envoi : fin du thread d'envoi des commandes au serveur]");
    }//run
}//class

class ClientReceive extends Thread{
    // class responsible for reading lines of text intended for a 
     // tcp client passed as parameter

    private Socket client;    // tcp client

     // manufacturer
    public ClientReceive(Socket client){
         // we note the tcp client
        this.client=client;
    }//manufacturer

     // thread Run method
    public void run(){

        // local data
        BufferedReader IN=null;        // network read stream
        String réponse=null;        // server response

         // error management
        try{
             // create network read stream
            IN=new BufferedReader(new InputStreamReader(client.getInputStream()));
            // loop read text lines from IN stream
            while(true){
                 // network streaming
                réponse=IN.readLine();
                 // closed flow?
                if(réponse==null) break;
                // display
                System.out.println("<-- "+réponse);
            }//while
        }catch(Exception ex){
            // error
            System.err.println("Réception : L'erreur suivante s'est produite : " + ex.getMessage());
        }//catch
         // end - we close the feeds
        try{
            IN.close();client.close();
        }catch(Exception ex){}
         // signals the end of the thread
        System.out.println("[Réception : fin du thread de lecture des réponses du serveur]");
    }//run
}//class

8.2.2. The generic TCP server

Now we'll look at a server

  • that displays on the screen the commands sent by its clients
  • and sends them, in response, the lines of text typed on the keyboard by a user. It is therefore the user who acts as the server.

The program is launched by: java genericTCPserver listeningPort, where listeningPort is the port to which clients must connect. Client service will be handled by two threads:

  • one thread dedicated exclusively to reading the text lines sent by the client
  • a thread dedicated exclusively to reading the responses typed on the keyboard by the user. This thread will signal, using the `fin` command, that it is closing the connection with the client.

The server creates two threads per client. If there are n clients, there will be 2n active threads at the same time. The server itself never stops unless the user presses Ctrl-C on the keyboard. Let’s look at a few examples.

The server is running on port 100, and we use the generic client to communicate with it. The client window looks like this:


E:\data\serge\MSNET\c#\réseau\client tcp générique> java clientTCPgenerique localhost 100
Commandes :
commande 1 du client 1
<-- réponse 1 au client 1
commande 2 du client 1
<-- réponse 2 au client 1
fin
L'erreur suivante s'est produite : Impossible de lire les données de la connexion de transport.
[fin du thread de lecture des réponses du serveur]
[fin du thread d'envoi des commandes au serveur]

Lines beginning with <-- are those sent from the server to the client; the others are from the client to the server. The server window is as follows:


Dos> java serveurTCPgenerique 100
Serveur générique lancé sur le port 100
Thread de lecture des réponses du serveur au client 1 lancé
1 : Thread de lecture des demandes du client 1 lancé
<-- commande 1 du client 1
réponse 1 au client 1
1 : <-- commande 2 du client 1
réponse 2 au client 1
1 : [fin du Thread de lecture des demandes du client 1]
fin
[fin du Thread de lecture des réponses du serveur au client 1]

Lines beginning with <-- are those sent from the client to the server. Lines N: are those sent from the server to client N. The server above is still active even though client 1 has finished. We launch a second client for the same server:


Dos> java clientTCPgenerique localhost 100
Commandes :
commande 3 du client 2
<-- réponse 3 au client 2
fin
L'erreur suivante s'est produite : Impossible de lire les données de la connexion de transport.
[fin du thread de lecture des réponses du serveur]
[fin du thread d'envoi des commandes au serveur]

The server window then looks like this:


Dos> java serveurTCPgenerique 100
Serveur générique lancé sur le port 100
Thread de lecture des réponses du serveur au client 1 lancé
1 : Thread de lecture des demandes du client 1 lancé
<-- commande 1 du client 1
réponse 1 au client 1
1 : <-- commande 2 du client 1
réponse 2 au client 1
1 : [fin du Thread de lecture des demandes du client 1]
fin
[fin du Thread de lecture des réponses du serveur au client 1]
Thread de lecture des réponses du serveur au client 2 lancé
2 : Thread de lecture des demandes du client 2 lancé
<-- commande 3 du client 2
réponse 3 au client 2
2 : [fin du Thread de lecture des demandes du client 2]
fin
[fin du Thread de lecture des réponses du serveur au client 2]
^C

Now let's simulate a web server by running our generic server on port 88:


Dos> java serveurTCPgenerique 88
Serveur générique lancé sur le port 88

Now let’s open a browser and request the URL http://localhost:88/exemple.html. The browser will then connect to port 88 on the localhost machine and request the /example.html page:

Image

Now let’s look at our server window:

Dos>java serveurTCPgenerique 88
Serveur générique lancé sur le port 88
Thread de lecture des réponses du serveur au client 2 lancé
2 : Thread de lecture des demandes du client 2 lancé
<-- GET /exemple.html HTTP/1.1
<-- Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*
<-- Accept-Language: fr
<-- Accept-Encoding: gzip, deflate
<-- User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705; .NET CLR 1.0.2
914)
<-- Host: localhost:88
<-- Connection: Keep-Alive
<--

This reveals the HTTP headers sent by the browser. This allows us to gradually learn about the HTTP protocol. In a previous example, we created a web client that sent only the GET command. That was sufficient. Here we see that the browser sends other information to the server. This information is intended to tell the server what type of client it is dealing with. We also see that the HTTP headers end with a blank line.

Let’s craft a response for our client. The user at the keyboard is the actual server here and can manually craft a response. Recall the response sent by a web server in a previous example:

<-- HTTP/1.1 200 OK
<-- Date: Mon, 13 May 2002 07:30:58 GMT
<-- Server: Apache/1.3.12 (Unix)  (Red Hat/Linux) PHP/3.0.15 mod_perl/1.21
<-- Last-Modified: Wed, 06 Feb 2002 09:00:58 GMT
<-- ETag: "23432-2bf3-3c60f0ca"
<-- Accept-Ranges: bytes
<-- Content-Length: 11251
<-- Connection: close
<-- Content-Type: text/html
<--
<-- <html>

Let's try to provide a similar response:

...
<-- Host: localhost:88
<-- Connection: Keep-Alive
<--
2 : HTTP/1.1 200 OK
2 : Server: serveur tcp generique
2 : Connection: close
2 : Content-Type: text/html
2 :
2 : <html>
2 :   <head><title>Serveur generique</title></head>
2 :   <body>
2 :     <center>
2 :       <h2>Reponse du serveur generique</h2>
2 :     </center>
2 :    </body>
2 : </html>
2 : fin
L'erreur suivante s'est produite : Impossible de lire les données de la connexion de transport.
[fin du Thread de lecture des demandes du client 2]
[fin du Thread de lecture des réponses du serveur au client 2]

Lines beginning with 2: are sent from the server to client #2. The end command closes the connection from the server to the client. In our response, we have limited ourselves to the following HTTP headers:

HTTP/1.1 200 OK
2 : Server: serveur tcp generique
2 : Connection: close
2 : Content-Type: text/html
2 :

We do not specify the size of the file we are sending (Content-Length), but simply indicate that we will close the connection (Connection: close) after sending it. This is sufficient for the browser. Upon seeing the connection closed, it will know that the server’s response is complete and will display the HTML page that was sent to it. The page is as follows:

2 : <html>
2 :   <head><title>Serveur generique</title></head>
2 :   <body>
2 :     <center>
2 :       <h2>Reponse du serveur generique</h2>
2 :     </center>
2 :    </body>
2 : </html>

The user then closes the connection to the client by typing the "fin" command. The browser then knows that the server's response is complete and can display it:

Image

If you select View/Source above to see what the browser received, you get:

Image

that is, exactly what was sent from the generic server.

The code for the generic TCP server is as follows:

// paquetages
import java.io.*;
import java.net.*;

public class serveurTCPgenerique{

    // programme principal
    public static void main (String[] args){

    // reçoit le port d'écoute des demandes des clients
    // crée un thread pour lire les demandes du client
    // celles-ci seront affichées à l'écran
    // crée un thread pour lire des commandes tapées au clavier
    // celles-ci seront envoyées comme réponse au client
    // le tout se termine avec la commande fin tapée au clavier

    final String syntaxe="Syntaxe : pg port";
  // variable d'instance
         // y-a-t-il un argument
     if(args.length != 1)
        erreur(syntaxe,1);

        // le port doit être entier >0
        int port=0;
        boolean erreurPort=false;
        Exception E=null;
        try{
            port=Integer.parseInt(args[0]);
        }catch(Exception e){
            E=e;
            erreurPort=true;
        }
        erreurPort=erreurPort || port <=0;
        if(erreurPort)
            erreur(syntaxe+"\n"+"Port incorrect ("+E+")",2);

     // on crée le servive d'écoute
    ServerSocket ecoute=null;
    int nbClients=0;    // nbre de clients traités
        try{
            // on crée le service
            ecoute=new ServerSocket(port);
            // suivi
            System.out.println("Serveur générique lancé sur le port " + port);

            // boucle de service aux clients
            Socket client=null;
            while (true){ // boucle infinie - sera arrêtée par Ctrl-C
                // attente d'un client
                client=ecoute.accept();

                // le service est assuré des threads séparés
                nbClients++;

                // on crée les threads de lecture/écriture
        new ServeurSend(client,nbClients).start();
        new ServeurReceive(client,nbClients).start();

                // on retourne à l'écoute des demandes
            }// fin while
        }catch(Exception ex){
            // on signale l'erreur
            erreur("L'erreur suivante s'est produite : " + ex.getMessage(),3);
        }//catch
    }// fin main

    // affichage des erreurs
    public static void erreur(String msg, int exitCode){
        // affichage erreur
        System.err.println(msg);
        // arrêt avec erreur
        System.exit(exitCode);
    }//erreur
}//classe

class ServeurSend extends Thread{
    // classe chargée de lire des réponses tapées au clavier
    // et de les envoyer à un client via un client tcp passé au constructeur

    Socket client;    // le client tcp
    int numClient;        // n° de client

    // constructeur
    public ServeurSend(Socket client, int numClient){
        // on note le client tcp
        this.client=client;
        // et son n°
        this.numClient=numClient;
    }//constructeur

    // méthode Run du thread
    public void run(){

        // données locales
        PrintWriter OUT=null;        // flux d'écriture réseau
        String réponse=null;        // réponse lue au clavier
    BufferedReader IN=null;    // flux clavier

        // suivi
        System.out.println("Thread de lecture des réponses du serveur au client "+ numClient + " lancé");
        // gestion des erreurs
        try{
            // création du flux d'écriture réseau
            OUT=new PrintWriter(client.getOutputStream(),true);
      // création du flux clavier
      IN=new BufferedReader(new InputStreamReader(System.in));
            // boucle saisie-envoi des commandes
            while(true){
                // identification client
                System.out.print("--> " + numClient + " : ");
                // lecture réponse tapée au clavier
                réponse=IN.readLine().trim();
                // fini ?
                if (réponse.toLowerCase().equals("fin")) break;
                // envoi réponse au serveur
                OUT.println(réponse);
                // réponse suivante
            }//while
        }catch(Exception ex){
            // erreur
            System.err.println("L'erreur suivante s'est produite : " + ex.getMessage());
        }//catch
        // fin - on ferme les flux
        try{
            OUT.close();client.close();
        }catch(Exception ex){}
        // on signale la fin du thread
        System.out.println("[fin du Thread de lecture des réponses du serveur au client "+ numClient+ "]");
    }//run
}//classe

class ServeurReceive extends Thread{
    // classe chargée de lire les lignes de texte envoyées au serveur 
    // via un client tcp passé au constructeur

    Socket client;    // le client tcp
    int numClient;        // n° de client

    // constructeur
    public ServeurReceive(Socket client, int numClient){
        // on note le client tcp
        this.client=client;
        // et son n°
        this.numClient=numClient;
    }//constructeur

    // méthode Run du thread
    public void run(){

        // données locales
        BufferedReader IN=null;        // flux lecture réseau
        String réponse=null;        // réponse serveur

        // suivi
        System.out.println("Thread de lecture des demandes du client "+ numClient + " lancé");
        // gestion des erreurs
        try{
            // création du flux lecture réseau
            IN=new BufferedReader(new InputStreamReader(client.getInputStream()));
            // boucle lecture lignes de texte du flux IN
            while(true){
                // lecture flux réseau
                réponse=IN.readLine();
                // flux fermé ?
                if(réponse==null) break;
                // affichage
                System.out.println("<-- "+réponse);
            }//while
        }catch(Exception ex){
            // erreur
            System.err.println("L'erreur suivante s'est produite : " + ex.getMessage());
        }//catch
        // fin - on ferme les flux
        try{
            IN.close();client.close();
        }catch(Exception ex){}
        // on signale la fin du thread
        System.out.println("[fin du Thread de lecture des demandes du client "+ numClient+"]");
    }//run
}//classe

8.3. JAVASCRIPT

In this section, we present three examples of using JavaScript in web pages. We focus on form handling, but JavaScript can do much more.

8.3.1. Retrieving information from a form

The example below shows how to retrieve data entered by the user in a form within the browser. This generally allows for pre-processing before sending the data to the server.

8.3.1.1. The form

We have a form containing the most common components and a "Display" button that allows you to view the user's entries.

Image

8.3.1.2. The code

<html>

  <head>
    <title>Un formulaire traité par Javascript</title>
    <script language="javascript">
      function afficher(){
        // affiche dans une liste les infos du formulaire

        // on efface d'abord
        effacerInfos();

        // on affiche la valeur des # champs
        with(document.frmExemple){
          // champ caché
          ecrire("champ caché="+cache.value);
          // champ textuel simple
          ecrire("champ textuel simple="+simple.value);
          // champ textuel multiple
          ecrire("champ textuel multiple="+lignes.value);
          // boutons radio
          for(i=0;i<radio.length;i++){
            texte="radio["+i+"]="+radio[i].value;
            if(radio[i].checked) texte+=", coché";
            ecrire(texte);
          }//for
          //  cases à cocher
          for(i=0;i<qcm.length;i++){
            texte="qcm["+i+"]="+qcm[i].value;
            if(qcm[i].checked) texte+=", coché";
            ecrire(texte);
          }//for
          //liste déroulante
          ecrire("index sélectionné dans le menu="+menu.selectedIndex);
          for(i=0;i<menu.length;i++){
            texte="menu["+i+"]="+menu.options[i].text;
            if(menu.options[i].selected) texte+=",sélectionné";
            ecrire(texte);
          }//for
          //liste à choix multiple
          for(i=0;i<lstVoitures.length;i++){
            texte="lstVoitures["+i+"]="+lstVoitures.options[i].text;
            if(lstVoitures.options[i].selected) texte+=",sélectionné";
            ecrire(texte);
          }//for
          //mot de passe
          ecrire("mot de passe="+passwd.value);
        }//with
      }//function

      function ecrire(texte){
        // écrit texte dans la liste des infos
        frmInfos.lstInfos.options[frmInfos.lstInfos.length]=new Option(texte);
      }//écrire

      function effacerInfos(){
        frmInfos.lstInfos.length=0;
      }//effacerInfos
    </script>
  </head>

  <body bgcolor="#C0C0C0" onload="afficher()">
    <center>
     <h2>Un formulaire traité par Javascript</h2>
    <hr>
    <form method="POST" name="frmExemple">
        <input type="hidden" name="cache" value="secret">
        <table border="0">
        <tr>
            <td align="center">Un champ textuel simple</td>
            <td align="center" width="100">&nbsp;</td>
            <td align="center">Un champ textuel sur plusieurs lignes</td>
        </tr>
        <tr>
            <td align="center"><input type="text" size="20" name="simple"></td>
            <td align="center" width="100">&nbsp;</td>
            <td align="center">
              <textarea name="lignes" rows="2" cols="40">Ce texte est modifiable</textarea>
            </td>
        </tr>
    </table>
    <table border="0">
      <tr>
       <td><strong>Des boutons radio :</strong></td>
        <td>
          <input type="radio" checked name="radio" value="FM">FM
        </td>
        <td>
          <input type="radio" name="radio" value="GO">GO
        </td>
        <td>
          <input type="radio" name="radio" value="PO">PO
        </td>
        <td>&nbsp;</td>
        <td><strong>Des choix multiples :</strong></td>
        <td>
          <input type="checkbox" name="qcm" value="un">un
        </td>
        <td>
          <input type="checkbox" name="qcm" value="deux">deux
        </td>
        <td>
          <input type="checkbox" name="qcm" value="trois">trois
        </td>
     </tr>
    </table>
    <table border="0">
      <tr>
       <td>Un menu déroulant : </td>
        <td>
          <select name="menu" size="1">
             <option>50 F</option>
              <option>60 F</option>
              <option>70 F</option>
              <option>100 F</option>
            </select>
        </td>
          <td>Une liste :</td>
           <td>
             <select name="lstVoitures" multiple size="3">
                <option>Renault</option>
                <option>Citroën</option>
                <option>Peugeot</option>
                <option>Fiat</option>
                <option>Audi</option>
              </select>
           </td>
        </tr>
    </table>
    <table border="0">
        <tr>
            <td>Un mot de passe : </td>
            <td><input type="password" size="21" name="passwd"></td>
            <td>&nbsp;</td>
            <td>Un champ de contexte caché : </td>
        </tr>
    </table>
    </form>
    <hr>
    <h2>Informations du formulaire</h2>
    <form name="frmInfos">
      <table>
        <tr>
          <td><input type="button" value="Effacer" onclick="effacerInfos()"></td>
          <td>
            <select name="lstInfos" multiple size="3">
            </select>
          </td>
          <td>
            <input type="button" name="cmdAfficher" value="Afficher" onclick="afficher()">
          </td>
        </tr>
    </form>
  </body>
</html>

8.3.2. Regular Expressions in JavaScript

On the browser side, JavaScript can be used to validate user-entered data before sending it to the server. Here is a program to test these regular expressions.

8.3.2.1. The test page

Image

8.3.2.2. The page code

<html>

  <head>
    <title>Les expressions régulières en Javascript</title>
    <script language="javascript">
      function afficherInfos(){
        with(document.frmRegExp){
          // qq chose à faire ?
          if (! verifier()) return;
          // c'est bon - on efface les résultats précédents
          effacerInfos();
          // vérification du modèle
          modele=new RegExp(txtModele.value);
          champs=modele.exec(txtChaine.value);
          if(champs==null)
            // pas de correspondance entre modèle et chaîne
            ecrireInfos("pas de correspondance");
          else{
            // correspondance - on affiche les résultats obtenus
            ecrireInfos("Il y a correspondance");
            for(i=0;i<champs.length;i++)
              ecrireInfos("champs["+i+"]=["+champs[i]+"]");
          }//else
        }//with
      }//function

      function ecrireInfos(texte){
        // écrit texte dans la liste des infos
        document.frmRegExp.lstInfos.options[document.frmRegExp.lstInfos.length]=new Option(texte);
      }//écrire

      function effacerInfos(){
        frmRegExp.lstInfos.length=0;
      }//effacerInfos

      function jouer(){
        // teste le modèle contre la chaîne dans l'exemple choisi
        with(document.frmRegExp){
          txtModele.value=lstModeles.options[lstModeles.selectedIndex].text
          txtChaine.value=lstChaines.options[lstChaines.selectedIndex].text
          afficherInfos();
        }//with
      }//jouer

      function ajouter(){
        //ajoute le test courant aux exemples
        with(document.frmRegExp){
          // qq chose à faire ?
          if (! verifier()) return;
          // ajout
          lstModeles.options[lstModeles.length]=new Option(txtModele.value);
          lstChaines.options[lstChaines.length]=new Option(txtChaine.value);
          // raz saisies
          txtModele.value="";
          txtChaine.value="";
        }//with
      }//ajouter

      function verifier(){
        // vérifie que les champs de saisie sont non vides
        with(document.frmRegExp){
          champs=/^\s*$/.exec(txtModele.value);
          if(champs!=null){
            alert("Vous n'avez pas indiqué de modèle");
            txtModele.focus();
            return false;
          }//if
          champs=/^\s*$/.exec(txtChaine.value);
          if(champs!=null){
            alert("Vous n'avez pas indiqué de chaîne de test");
            txtChaine.focus();
            return false;
          }//if
          // c'est bon
          return true;
        }//with
      }//verifier
    </script>
  </head>

  <body bgcolor="#C0C0C0">
    <center>
     <h2>Les expressions régulières en Javascript</h2>
    <hr>
    <form name="frmRegExp">
      <table>
        <tr>
          <td>Expression régulière</td>
          <td>Chaîne de test</td>
        </tr>
        <tr>
          <td><input type="text" name="txtModele" size="20"></td>
          <td><input type="text" name="txtChaine" size="20"></td>
        </tr>
        <tr>
          <td>
            <input type="button" name="cmdAfficher" value="Jouer le test" onclick="afficherInfos()">
          </td>
          <td>
            <input type="button" name="cmdAjouter" value="Ajouter aux exemples" onclick="ajouter()">
          </td>
        </tr>
      </table>
      <hr>
      <h2>Résultats de l'instruction champs=expression régulière.exec(chaine)</h2>
      <table>
        <tr>
          <td>
            <select name="lstInfos" size="3">
            </select>
          </td>
        </tr>
      </table>
      <hr>
      <h2>Exemples</h2>
      <table>
 <tr>
          <td align="center">Modèles</td>
          <td align="center">Chaînes</td>
        </tr>
        <tr>
          <td>
            <select name="lstModeles" size="1">
              <option>^\d+$</option>
              <option>^(\d+) (\d+)$</option>
              <option>^(\d+)(.*)(\d+)$</option>
              <option>^(\d+)(\s+)(\d+)$</option>
            </select>
          </td>
          <td>
            <select name="lstChaines" size="1">
              <option>67</option>
              <option>56 84</option>
              <option>45abcd67</option>
              <option>45   67</option>
            </select>
          </td>
          <td>
            <input type="button" name="cmdJouer" value="Jouer l'exemple" onclick="jouer()">
          </td>
        </tr>
    </form>
  </body>
</html>

8.3.3. List Management in JavaScript

8.3.3.1. The form

Image

8.3.3.2. The code

<html>

  <head>
    <title>Les listes en Javascript</title>

    <script language="javascript">
      // ajouter
      function ajouter(L1,L2,T){
        // ajoute la valeur du champ T aux listes L1,L2
          // qq chose à faire ?
          champs=/^\s*$/.exec(T.value);
          if(champs!=null){
            // le champ est vide
            alert("Vous n'avez pas indiqué la valeur à ajouter");
            txtElement.focus();
            return;
          }//if
          // on ajoute l'élément
          L1.options[L1.length]=new Option(T.value);
          L2.options[L2.length]=new Option(T.value);
          T.value="";
      }//ajouter

      //vider
      function vider(L){
        // vide la liste L
        L.length=0;
      }//vider

     //transfert
      function transfert(L1,L2,simple){
        //transfére dans L2 les éléments sélectionnés dans la liste L1

        // qq chose à faire ?
        // index de l'élément sélectionné dans L1
        index1=L1.selectedIndex;
        if(index1==-1){
          alert("Vous n'avez pas sélectionné d'élément");
          return;
        }//if
        // quel est le mode de sélection des éléments des listes
        if(simple){ // sélection simple
          element1=L1.options[index1].text;
          //ajout dans L2
          L2.options[L2.length]=new Option(element1);
          //suppression dans L1
          L1.options[index1]=null;
        }//simple
        if(! simple){ //sélection multiple
          //on parcourt la liste 1 en sens inverse
          for(i=L1.length-1;i>=0;i--){
            //élément sélectionné ?
            if(L1.options[i].selected){
              //on l'ajoute à L2
              L2.options[L2.length]=new Option(L1.options[i].text);
              //on le supprime de L1
              L1.options[i]=null;
            }//if
          }//for i
        }//if ! simple
      }//transfert
   </script>
  </head>

  <body bgcolor="#C0C0C0">
    <center>
     <h2>Les listes en Javascript</h2>
    <hr>
    <form name="frmListes">
      <table>
        <tr>
          <td>
            <input type="button" name="cmdAjouter" value="Ajouter" onclick="ajouter(lst1A,lst1B,txtElement)">
          </td>
          <td>
            <input type="text" name="txtElement">
          </td>
        </tr>
      </table>
      <table>
        <tr>
          <td align="center">liste 1</td>
          <td align="center"><input type="button" value=">>" onclick="transfert(lst1A,lst2A,true)"</td>
          <td align="center"><input type="button" value="<<" onclick="transfert(lst2A,lst1A,true)"</td>
          <td align="center">liste 2</td>
          <td width="30"></td>
          <td align="center">liste 1</td>
          <td align="center"><input type="button" value=">>" onclick="transfert(lst1B,lst2B,false)"</td>
          <td align="center"><input type="button" value="<<" onclick="transfert(lst2B,lst1B,false)"</td>
          <td align="center">liste 2</td>
        </tr>
         <tr>
          <td></td>
          <td align="center">
            <select name="lst1A" size="5">
            </select>
          </td>
          <td align="center">
            <select name="lst2A" size="5">
            </select>
          </td>
          <td></td>
          <td></td>
          <td></td>
          <td align="center">
            <select name="lst1B" size="5" multiple >
            </select>
          </td>
          <td align="center">
            <select name="lst2B" size="5" multiple>
            </select>
          </td>

        </tr>
        <tr>
          <td></td>
          <td align="center"><input type="button" value="Vider" onclick="vider(lst1A)"</td>
          <td align="center"><input type="button" value="Vider" onclick="vider(lst2A)"</td>
          <td></td>
          <td></td>
          <td></td>
          <td align="center"><input type="button" value="Vider" onclick="vider(lst1B)"</td>
          <td align="center"><input type="button" value="Vider" onclick="vider(lst2B)"</td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td colspan="2"><strong>Sélection simple</strong></td>
          <td></td>
          <td></td>
          <td></td>
          <td colspan="2"><strong>Sélection multiple</strong></td>
          <td></td>
        </tr>

      </table>
      <hr>
    </form>
  </body>
</html>