In this exercise, you will write a guest book applet , which can be put on your home page and which will allow visitors to register themselves as guests. You will also write a threaded guest book server , which stores a set of registered users in the file database you wrote in the previous exercise. When a guest submits his/her information, the applet will contact the server and ask it to store the information the user has entered.
You can read more about Java clients and servers using sockets in the custom networking part of the Java Tutorial, especially the All About Sockets section.
Due to security restrictions, Java applets (such as the guest book applet you will write) are only allowed to contact the server computer from which they were downloaded - they are is not even allowed to contact ports on the computer on which they are running.
In this lab, the guest book server you write will be running on your own computer. The applet can only contact it if it is downloaded from the same computer. Therefore, you must have a web server running on your own computer.
In this course, we are using the Java Web Server (previously called Jeeves) which is written completely in Java. How you use the server is described in more detail below.
The server is already installed on the lab computers. If you are doing your labs at home, you must install a web server supporting servlets. You can download the Java Web Server from here (use version 1.0.3; it is free for educational use). Note that you will be writing servlets that are pre-configured in the Java Web Server installed in the PC lab rooms. If you want to do this at home, you must configure everything yourself - unfortunately, we do not have the time to answer your questions about installing software on your own computers. If you have problems, use the computers in the PC lab rooms.
Normally, if you were running a 'permanent' web server, the Java Web Server would run as a Windows NT service - that is, it would start automatically when you turn on the computer.
In this course, since the web server should not be permanent, you must start it manually. Unfortunately, this is more complicated than it should be.
To start the server, you must first start a command prompt (the Windows Start menu, then Programs , then Command Prompt ). In the command prompt window, you type the following:
c:
cd \tddi48\javawebserver1.0.3\bin
httpdnjr
All log messages written by the web server will be shown in the command prompt window. Note that as the server starts up, it will write 10-20 lines of messages; this is normal.
After this, you must start a web browser (such as HotJava or Netscape Navigator) and open the address http://localhost:9090 - this takes you to the administration interface for the web server. Log in as administrator (user name 'admin', password 'admin'). You will see three services - the Web Service, the Proxy Service and the Administration Service. Single-click on the Web Service (port 8080) and click the "Start" button.
To stop the Java Web Server completely, press Ctrl-C in the command prompt window. You can also stop and restart it from the administration interface as described above.
(This section describes approximately how your network communication protocol will work, but you will not implement it until part 3.)
The information that the user enters must be sent to the server in some way, and the server must be able to reply with a status message (or error message) of some kind.
To be able to do this, we must first create a two-way network connection between the client and the server. This is done using sockets , which you can read about in the custom networking part of the Java Tutorial.
Once we have a socket connection, we can use its getInputStream() and getOutputStream() methods to get ordinary input and output streams that we can read from and write to. But what should we read and write?
In previous versions of this lab, when we used Java 1.0.2, we sent the user information as text strings separated by some field delimiter, but this was very cumbersome. Fortunately, object serialization, which you used in the previous exercise, works as well with socket input/output streams as with files. This is what you will use in the guest book applet/server.
The details of your network communication protocol are up to you. However, the following things will be necessary:
This can be achieved in two ways: Either the server can return a readable error message string that is shown to the user by the applet, or it can return some kind of error code (an integer or a string) which is translated into an error message by the applet. An error code is probably better, since it is more well-defined and can be ``understood'' by the applet in case it wants to react to different errors in different ways.
Create a new applet - the guest book applet - in the client package. In the lab description, we will refer to this applet as GuestBookApplet.
You will need to design the user interface for your guest book applet. There should be a text field for the name, one for the e-mail address, and so on. Make sure that the components you use are appropriate - for example, the ``want to be informed'' field is boolean and should therefore be a checkbox, and since the general comments may be long, that field should be a TextArea and not a TextField. Use a layout manager (for example, GridBagLayout) to position the fields in the applet!
There should be at least five action buttons available in the applet:
You may want also want to have a ``clear all information'' button, and give the fields default values (such as the string ``http://'' in the web page field).
You should also include an information field where the applet can show information, such as error messages, to the user.
Write a multi-threaded guest book server that can accept user information from a network socket and store it in a user database.
Since classes implementing UserDatabase are only required to enforce threadsafe operations within the same UserDatabase object, you should create your FileDatabase only once, when the server is created, and use it in all server threads. You also want to be able to reuse your guest book server class with different kinds of UserDatabase without making changes to the server. Therefore, the constructor of the guest book server should take a UserDatabase as an argument.
You can then create a FileDatabaseServer class as follows:
package server;
public class FileDatabaseServer {
public static void main(String[] args) {
new ThreadedServer(new FileDatabase(/* your code here */));
}
}
In this exercise, you will implement the functionality for the ``submit'' button in the applet. When the user presses ``submit'', the following should happen:
Note that the applet and the server threads cannot create their ObjectInputStreams and ObjectOutputStreams in the same order. The reason for this is that when you create an ObjectOutputStream, it wants to send a header before the constructor returns, and when you create an ObjectInputStream, it waits for a header before it returns. Therefore, if you start with creating two streams of the same kind, they will both be either trying to write a header or waiting for a header.
To test the guest book, create a JAR (Java Archive) file containing all classes in your
project; this can be done easily using Project/JAR in Visual Café. Create an HTML page
that contains your applet and put it in the z:\public_html directory (if you don't
have one, you must create one); this is where the Java Web Server will look for its
documents. The HTML page should refer to your client.GuestBookApplet class and
use the JAR archive that you just created (the ARCHIVE attribute of the APPLET tag); this
archive must also be placed in z:\public_html.
You can read more about JAR in the JAR documentation in the JDK 1.1 documentation.
Start the guest book server on your computer - you may want to do this from a command line prompt.
Try to add a few users. Add users with the same key (e-mail address, or whatever field you used as a key); this should fail. Add several users with different keys. Look at the database file you created (using the Windows Explorer); the file should grow when you add more users.
If you update and recompile your code, don't forget to re-create the JAR archive!