Java
Understanding the “Java”
environments
Java is more than a language
Language is C-like
Same control constructs
Similar Syntax
Things do what you think they would.
Java “packages” allow you to manage
related classes as a unit.
JVM can run almost anywhere.
Java environments
Applications
Command Line
Local GUI
Applets- run in browser
Servelets – run in server
Java Server pages – servelets that start as
java embedded into HTML.
Security issues
Concept Review
Java environments designed for the
Web.
Java works everywhere imaginable
Only real competition is Microsoft .NET
Similar Architectures.
Applications: CLI and GUI
JVM directly Application:
on Desktop OS Class file on
local Disk
Desktop Machine
Applets: Java in the Browser
JVM directly Application
on Desktop OS
Desktop Machine Server
Browser
Web
Applet: Class Server
JVM Inside Browser file on Web
Server
Java on the Server
JVM runs Tomcat
Web Server
Desktop
Machine Server
Browser
JVM Web
Server Nescape
HTML
Java on the Server
8080
Tomcat JVM Class file
Java Servelet on Server
Desktop
Machine Server
Browser
PHP
Interpreter Apache
HTML 80
Java on the Server
Desktop
Machine Server
Class file
Browser Java Servelet on Server
8080
Tomcat JVM
HTML 80
Packages, Collections,
Containers, and Environments
JAR
Foundation classes
JavaBeans
Collections
JINI
IDL– ORBs
JDBC
Applications and Applets
Getting the Java environment to work.
www.Java.sun.com
Test Application
Test Applet
System Architecture
Getting the Java Environment
Free to use
Not bad tools
Several IDE’s
It took me about 4 hours to get things
all sorted out and take the “Getting
Started” tutorials.
Test Application: Hello World!
HelloWorldApp.java
Compile to bytecodes
Execute on virtual machine
Command line stuff is easier to
understand.
GUI is a bit more difficult
Command Line:
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Compile, Run
>javac HelloWorldApp.java
>java HelloWorldApp
Hello World!
>
GUI Window:
import java.awt.*;
public class HelloGUI extends Frame {
private static Font f;
Fonts and positioning
private void drawCenteredString (Graphics g, String s){
Dimension d = getSize();
FontMetrics fm=getFontMetrics(f);
int x=(d.width-fm.stringWidth(s))/2;
int y=(d.height-((d.height-fm.getHeight())/2);
g.drawString(s,x,y);
}
Paint method:
public void paint(Graphics g) {
g.setFont(f);
g.setColor(Color.red);
drawCenteredString(g,”Hello in a window!”);
}
Main program:
public static void main(String[] args) {
Frame theFrame = new HelloGUI();
theFrame.setTitle (“Java Hello World!”);
theFrame.setSize (250,100);
f=new Font(“Helvetica”,Font.BOLD, 24);
theFrame.show();
}
Applet:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloAppet extends Applet {
public void paint (Graphics theGraphics); {
theGraphics.drawString(“Hello in Applet!”,0,50);
}
}
HTML to invoke Applet:
Tomcat Demo http://localhost:8080
Browser Desktop
Machine:
localhost
X.jsp
JSP Compiler
X.java
Java Compiler
8080 Tomcat JVM X.class
HTML Java Servelet