Client-Server Applications

Adistributed application is several programs collaborating to solve the same problem (running on the same computer or different computers connected by a network). The most popular types of distributed applications are client-server applications. In a client-server application certain programs are designated as servers, while others are clients. Clients request services from the servers. Servers are often multi threaded programs that spawn threads to service client requests. File and database servers are early examples of servers.





World Wide Web (WWW)

WWW is an example of a client-server application. Web servers manage collections of web pages (HTML files). Web clients (browsers) request and display web pages:

A browser identifies the web page it wants using a Universal Resource Locator (URL). A URL has the form:

PROTOCOL://HOST/PATH For example, the URL for a web page has the form: http://www.ncsa.uiuc.edu/demoweb/url-primer.html http stands for "hyper text transport protocol". (Java has a predefined class called URL for representing URLs.)
 
 

HTML "Review"

A web page is a text file that uses the hyper text markup language (HTML) to describe what a browser should display. Here is the contents of a file called planets.html:

<HTML>
<TITLE> Planets </TITLE>
<BODY>
Hello, Earth <BR>
Hello, Mars <BR>
Hello, Jupiter <BR>
Hello, Neptune <BR>
</BODY>
</HTML>
<HTML> ... </HTML>, <TITLE> ... </TITLE>, and <BODY> ... </BODY> are tags used by the browser to determine that the file contains html instructions, the title of the page, and the contents of the page. The <BR> tag tells the browser to insert a line break. Here is what planets.html looks like when displayed by a Netscape browser:







HTML Elements

Besides text, html files can contain other elements. An element usually appears between two tags:

<TAG> element </TAG> A tag can also have parameters: <TAG param1=value1 param2=value2 ... > Here are some examples:

images

<IMG SRC="image.gif"> Tables <table> ... </table> Anchors (links) <A HREF = "other.html"> this is a link </A> Forms <FORM ...> ... </FORM> Lists <UL>
   <LI> apples
   <LI> bananas
</UL>
There are many good HTML tutorials on the web. For example: http://www-pcd.stanford.edu/mogens/intro/tutorial.html

http://www.utoronto.ca/webdocs/HTMLdocs/NewHTML/htmlindex.html

Alternatively, Netscape's Composer (click the button in the lower right corner of the Navigator) is a WYSIWYG HTML editor.
 
 

The Applet-Servlet Framework

Sometimes the common features of a family of similar applications can be extracted and placed in a framework, which can then be customized to create different applications in the family. Typically, a framework is a library of related base classes which is customized by extension.

Java has created a framework for the client-server application family. Applet is the base class for all clients, and Servlet is the base class for all servers. The WWW is such a generic client-server application, that Applets piggy back on top of Applet-enabled web clients, and Servlets piggy back on top of Servlet-enabled web servers:

We will not discuss servlets, because we don't have access to a web server with a servlet engine, and servlets aren't part of JDK. If your interested, visit: http://java.sun.com/products/servlet/index.html
 
 

Applets

Recall that a panel is a borderless container. An applet is a special type of panel:

class Applet extends Panel
{
   public void init() {} // browser calls first time page is visited
   public void start() {} // browser calls each time page is visited
   public void stop() {} // browser calls each time page is replaced
   public void destroy() {} // browser calls when applet is flushed
   public String getParameter() { ... } // get params from web page
   public void showStatus(String msg) { ... } // show msg in browser's status bar
   public String getAppletInfo() { return null; } // author, version, etc.
   // etc.
}
An applet doesn't need a border because the web page that contains it is already bordered by the browser. Without a border, titles and menu bars don't make sense. Also, the height and width of the applet are specified as applet tag parameters, so don't use setSize(). Applets don't have a main() function, the browser takes care of this. Finally, applets don't need constructors. This is taken care of by init().

Security Rules

People naturally feel uncomfortable downloading and running programs. The dangers of viruses, worms, and Trojan horses are quite real. To combat these worries, Java enforces some rules regarding what Applets can and can't do. Different browsers may also add some rules. Basically, applet can't:

run local programs
 
 

communicate with any computer other than the originating host
 
 

read or write to the local file system
 
 

gather information about the local host except for Java version, type of OS, and file separator character used (/ or \)
 
 

pop up windows that don't carry warnings
 
 

Drawlet

Recall the mouse drawing application discussed earlier. Drawlet is essentially an applet version of the same program.

appletViewer version

The applet viewer is a tool that allows you to test your applets. Here are the commands I used:

C:\pearce\Java\Projects\paint>javac Drawlet.java
C:\pearce\Java\Projects\paint>appletViewer paint.html

Netscape 4.5 Version

You can try this out yourself if you have a modern version of Netscape. Click here.
 
 

Drawlet.java

Here's the code:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import pearce.java.util.*;
import java.applet.*;
 
 

public class Drawlet extends Applet
{
   private Vector drawing;      // points in drawing
   private int brushWidth;       // diameter of circle
   private int red, green, blue;   // color of circle

   AppletContext context;
   Frame myFrame;

   public void init()
   {
      addMouseMotionListener(new MouseMotionHandler());
      addMouseListener(new MouseEventHandler());
      drawing = new Vector();

     brushWidth = Tools.toInt(getParameter("brushWidth"));
      red = Tools.toInt(getParameter("red"));
      green = Tools.toInt(getParameter("green"));
      blue = Tools.toInt(getParameter("blue"));

     context = getAppletContext();

     // search ancestors in the component hierarchy until a frame is found
      Object parent = getParent();
      while (!(parent instanceof Frame))
         parent = ((Component) parent).getParent();
      myFrame = (Frame) parent; // EmbeddedAppletFrame in Netscape?
   }
 
 

   class MouseMotionHandler implements MouseMotionListener
   {
      public void mouseMoved(MouseEvent e) { }

      public void mouseDragged(MouseEvent e)
      {
        myFrame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
         Point p = e.getPoint();
         drawing.addElement(p);
        showStatus("Dragging");
         // to prevent flicker, only repaint a small rectangle
         repaint(p.x, p.y, brushWidth, brushWidth);
      }
   }
 
 

   class MouseEventHandler extends MouseAdapter
   {
      public void mouseClicked(MouseEvent e)
      {
         if (e.getClickCount() > 1)
         {
            drawing.removeAllElements();
            repaint(); // repaint entire window
         }
      }
      public void mouseReleased(MouseEvent e)
      {
        myFrame.setCursor(Cursor.getDefaultCursor());
         showStatus("");
      }
   }
 
 

public String getAppletInfo()
{
   String about = "Author: J. Pearce";
   about += ", version 1.0";
   return about;
}
 
 

   public void paint(Graphics g)
   {
      g.setColor(new Color(red, green, blue));
      for(int i = 0; i < drawing.size(); i++)
      {
         Point p = (Point)drawing.elementAt(i);
         g.fillOval(p.x, p.y, brushWidth, brushWidth);
      }
   }

} // Drawlet

paint.html

Applets are embedded in web pages like any other element, only using applet tags: <APPLET ... > ... </APPLET>

<HTML>
<TITLE> A Paint Program </TITLE>
<BODY>
Draw something with your mouse (version 11):<BR>
<APPLET CODE="Drawlet.class" WIDTH=200 HEIGHT=300>
<PARAM NAME=red VALUE="0">
<PARAM NAME=blue VALUE="0">
<PARAM NAME=green VALUE="255">
<PARAM NAME=brushWidth VALUE="5">
Sorry, your browser can't show applets.
</APPLET>
</BODY>
</HTML>