CSC 551: Web Programming Spring 2004: Java Overview
CSC 551: Web Programming Spring 2004: Java Overview
Java Overview
Design goals & features platform independence, portable, secure, simple, object-oriented,
Programming models applications vs. applets vs. servlets intro to applets libraries, comments, classes, inheritance applet tag in HTML applet parameters
1
Java
Java was developed at Sun Microsystems, 1995
originally designed for small, embedded systems in electronic appliances initial attempts used C++, but frustration at limitations/pitfalls recall: C++ = C + OOP features the desire for backward compatibility led to the retention of many bad features
note: these are desirable features for any modern language thus, Java has become very popular, especially when Internet related also, Sun distributes free compilers (JDK) and open source
Language features
simple
syntax is based on C++ (familiarity easier transition for programmers) removed many confusing and/or rarely-used features e.g., explicit pointers, operator overloading, automatic coercions added memory management (reference count/garbage collection hybrid)
object-oriented
OOP facilities similar C++, all methods are dynamically bound pure OOP everything is a class, no independent functions*
robust
lack of pointers and memory management avoids many headaches/errors libraries of useful, tested classes increases level of abstraction arrays & strings are ADTs, well-defined interfaces
3
architecture-neutral
portable
high-performance
4
multi-threaded
a thread is like a separate program, executing concurrently can write Java programs that deal with many tasks at once by defining multiple threads (same shared memory, but semi-independent execution) threads are important for multi-media, Web applications
secure
Java applications do not have direct access to memory locations memory accesses are virtual, mapped by JVM to physical locations downloaded applets cannot open, read, or write local files JVM also verifies authenticity of classes as they are loaded Sun claim: execution model enables virus-free*, tamper-free* systems
Java applets
important point: Java applets & applications look different!
if you want to define a stand-alone application, make an application requires public static void main function, similar to C++ main if you want to embed the code in a Web page, make an applet requires public void paint, public void init, can define dual-purpose programs, but tricky
when a Java applet is downloaded, the bytecode verifier of the JVM verifies to see if it contains bytecodes that open, read, write to local disk a Java applet can open a new window but they have Java logo to prevent them from being disguised as system window (e.g., to steal passwords) a Java applet is not allowed to connect back to other servers except the host this secure execution environment is called sand box model
there are no stand-alone functions in Java* must be stored in a file of same name with .java extension
e.g., HelloWorld.java
9
by default, does nothing paint(Graphics g): called to draw (after init) or redraw (after being obscured) here, the paint method is overridden to display text on the applet window
10
OBJECT tag
preferred for HTML 4, but not universally supported
<html> <!-- Dave Reed
hello1.html 3/20/04 -->
<head> <title>Hello World Page</title> </head> <body> <p> <applet code="HelloWorld.class" height=100 width=100> You must use a Java-enabled browser to view this applet. </applet> </p> </body> </html>
11
an applet can be embedded within HTML elements just like any other element useful for formatting and layout
Parameters in HTML
<html> <!-- Dave Reed hello3.html 3/20/04 --> <head> <title>Hello World Page</title> </head> <body> <p> <div align="center"> <table border=1> <tr><td> <applet code="HelloWorld1.class" height=35 width=300> <param name="name" value="Chris"> <param name="age" value=20> You must use a Java-enabled browser to view this applet. </applet>
13
Applet parameters
import java.awt.*; import java.applet.*; /** * This class displays a message based on parameters. */ public class HelloWorld1 extends Applet { public void paint(Graphics g) { String userName = getParameter("name"); int userAge = Integer.parseInt(getParameter("age")); String message1 = "Hello " + userName + "."; String message2 = "On your next birthday, you will be " + (userAge+1) + " years old."; g.drawString(message1, 10, 10); g.drawString(message2, 10, 30); } }