1 Active Web Document Technologies (Java, JavaScript) An Early

Document Sample
scope of work template
							                                                                 An Early Form of Continuous Update
                                                             • Problems with “Dynamic” web documents.
                                                                 – They are not good enough for all applications.
                                                                 – Information in the document is fixed when the
  Active Web Document Technologies                                 document is created.
          (Java, JavaScript)                                     – They would not work for stock price updates.
                                                             • Server push technique.
                                                                 – Server periodically sends new copies of a document.
                      Chapter 37
                                                                 – Server program must run continuously putting a
                                                                   large load on the server platform.
                                                                 – Server must run a separate copy of the generator
                                                                   program for each client.
                                                                 – Each client must maintain an active TCP connection.




Active Documents and Server Overhead                               Active Document Representation
• Active Document technique.
  – The computation is moved from the server to the
    browser (client).
  – An active document is really a computer program
    that is run locally by the browser. It is delivered to
    the browser just like a static document.
  – It is stored in a file on the server and changed only
    by programmer intervention.
  – The active document can be held in cache.
  – The browser must have the ability to run the code.
  – The active document may be translated into a
    compressed form to use less space.




                Java Technology                                       Java Programming Language
• Java was developed by Sun Microsystems.                    • High Level
• A Java Applet is an active document using Java             • General Purpose
  technology.                                                • Similar to C++
  – Programming Language -- Source code for Java                 – No operator overloading, no multiple inheritance, no
    applets.                                                       extensive automatic data coercions.
  – Runtime Environment -- Provides the facilities           •   Object Oriented
    needed to run a Java program.                            •   Dynamic - objects created at run time.
  – Class Library -- Large library of software already       •   Strongly Typed
    written to handle many common chores.
                                                             •   Statically Type Checked - at compile time.
                                                             •   Concurrent




                                                                                                                          1
         Java Run-Time Environment                                             Java Class Library
• Interpretative Execution                                     • Graphics Manipulation
    – A Java “compiler” translates a Java program into a           – Text, graphics, images, dialog boxes.
      machine-independent binary representation know as        • Low-Level Network I/O (Sockets)
      the Java Bytecode representation.                        • Interaction with a Web Server
    – An “interpreter” reads the bytecode and interprets the   • Run-Time System Access
      instructions for a specific target machine.
                                                                   – Create another thread
• Automatic Garbage Collection                                 • File I/O on the local machine
    – Automatic malloc() and free().
                                                               • Conventional Data Structures
•   Multithreaded Execution
                                                               • Event Capture
•   Internet Access                                                – Mouse clicks, etc.
•   Graphics Support                                           • Exception Handling
•   Machine Independence and Portability




                  Graphics Toolkit                                 Using Java Graphics on a Particular
• Together the run-time graphics support and the                               Machine
  graphics library are known as a graphics toolkit.            • A Java run-time environment include an
  Specifically this one is called the Abstract                   intermediate layer of software.
  Window Toolkit (AWT).                                            – Each function in this layer uses the computer’s own
    – The toolkit has classes for both low-level and high-           window system to implement one specific Java
      level manipulation.                                            graphics operation.
                                                                   – Before the applet executes, the run-time environment
                                                                     established a mapping between each Java graphics
                                                                     method and the appropriate intermediate function.




        Java Interpreters and Browsers                                    Compiling a Java Program
• A browser can contain one or more interpreters.              •   The Java compiler is called javac.
• A browser that runs Java has at least two of                 •   Java source code has extension .java.
  these: HTML and Java.                                        •   Java bytecode has extension .class.
• A Java interpreter is a virtual machine with                 •   A source file must contain exactly one public
  instruction pointer, registers, etc.                             class, which must use the same name as the
• The Java interpreter must be able to                             source file prefix.
  communicate with the HTTP client and the
  HTML interpreter in order to display objects on
  the screen.




                                                                                                                            2
                  Example Java Applet                                                               Example Java Applet (2)
import java.applet.*;
import java.awt.*;

public class clickcount extends Applet {
  int count;
  TextField f;
  public void init() {
    count = 0;
    add( new Button( “Click Here” ));
    f = new TextField(“Button not clicked.”);
    f.setEditable( False );
    add( f );
  }
  public boolean action( Event e, Object arg ) {
    if((( Button ) e.target ) .getLabel() == “Click Here” ) {
      count += 1;
      f.setText( “Button clicked “ + count + “ times.” );
    }                                                                           • Screen display before the button is clicked and
    return true;
  }                                                                               after it is clicked once.
}




                    Invoking an Applet                                                            Interaction with a Browser
• User supplies URL of the applet to a browser                                  • An applet can interact with both the HTTP client
  that understands Java.                                                          and the HTML interpreter in a browser.
    – http://www.server.edu/example/bbb.class                                   • Here is an example that uses the browser’s
• HTML document contains an applet tag.                                           HTTP client to retrieve documents and the
    – Specifies the location of the .class file.                                  browser’s HTML interpreter to display pages of
    – <applet codebase=“www.server.edu/example”                                   information.
      code=“bbb.class”>




                  Buttons.java Example                                                             Buttons.java Example (2)
import java.applet.*;                                                                     catch ( Exception ex ) {
import java.net.*;                                                                        // note: code to handle the exception goes here //
import java.awt.*;                                                                        }
                                                                                         }
public class buttons extends Applet {                                                    else if ((( Button ) e.target ).getLabel() == "Yang“ ) {
                                                                                           try {
 public void init() {                                                                         getAppletContext().showDocument( new URL (
   setBackground( Color.white );                                                        "http://www.netbook.cs.purdue.edu/cs363/lecture_notes/chap29/yang.html"));
   add(new Button( "Ying“ ));                                                              }
   add(new Button( "Yang“ ));                                                              catch ( Exception ex ) {
 }                                                                                         // note: code to handle the exception goes here //
                                                                                           }
  public boolean action( Event e, Object arg) {                                          }
    if ((( Button ) e.target ).getLabel() == "Ying“ ) {                                  return true;
      try {                                                                         }
         getAppletContext().showDocument( new URL (                             }
   "http://www.netbook.cs.purdue.edu/cs363/lecture_notes/chap29/ying.html“));
      }




                                                                                                                                                                     3
        Errors and Exception Handling                                          JavaScript Technology
• In Java, all errors produce exceptions.                           • Instead of compiling an applet, JavaScript
• An applet is required to handle such exceptions                     provides a scripting language, and arranges for a
  explicitly.                                                         browser to read and interpret a script in source
• The applet uses the try-catch construct.                            form.
• try {s} catch {e} {h}                                             • JavaScript can be intermixed with HTML.
   –   s = code to execute while looking for an exception.
   –   e = the exception to look for.
   –   h = the handler to invoke should e happen.
   –   If the handler is not specified, all exceptions are
       ignored.




                 JavaScript Example
<html> <head> <title> JavaScript counter </title>
<body onLoad=“return incrementcount(document.demo);”>
<script>
   var n;
   n = -1;
   function incrementcount(form) {
        n = n + 1;
        if(n==0) {
                s = “The button has not been clicked.”;
        }
        else {
                s = “The button has been clicked” + n + “times.”;
        }
        form.elements[“OutputArea”].value = s;
   }
</script>
<form name=“demo”>
   <input type=“button” name=“ButtonCLick” value=“Click here”
        onCLick=“incrementcount(this.form);”
   <textarea name=“OutputArea” rows=1 cols=42 </textarea>
</form>
</body> </html>




                                                                                                                          4

						
Other docs by msz78385
MyEclipse 6 Java 开发中文教程
Views: 29  |  Downloads: 1
Exploring the World of PROC SQL Joins
Views: 26  |  Downloads: 2
Java Procedural Programming
Views: 22  |  Downloads: 0
Java與JSP
Views: 34  |  Downloads: 0
Java and Databases
Views: 4  |  Downloads: 2
A Brief Introduction to Java 2
Views: 5  |  Downloads: 0
Pustaka Java
Views: 502  |  Downloads: 15