You are on page 1of 40

Applets & Servlets

31 October 2002- Group Meeting


Nelson Young
University of Alberta
Overview
 This presentation will provide an overview of the basics of
applets and servlets as well as their roles in web development
and enrichment. The presentation will answer the following
questions:
 What is an applet?
 How do applets work?
 What are the advantages and disadvantages of applets?
 What is a servlet?
 How do servlets work?
 What are the advantages and disadvantages of servlets?
 Is there a relationship between applets and servlets?

31 October 2002 Nelson Young 2


What is an applet?
 An applet is a Java program that is
embedded and ran in some other Java
interpreter program such as
 a Java technology-enabled browser
 Sun’s applet viewer program called

appletviewer

31 October 2002 Nelson Young 3


Java Interpreter Programs

31 October 2002 Nelson Young 4


How do applets works?
Step 1
 Download and install
Sun’s Java Software
Development Kit (SDK)
to allow compilation
and interpretation of
compiled class files
 Write the Java source
code for the applet in
any text editor.

31 October 2002 Nelson Young 5


How do applets work?
Step 2
 Compile the
source code to
obtain the
necessary
bytecodes known
as “.class” for
interpreting

31 October 2002 Nelson Young 6


How do applets work?
Step 3

Server

Web page
age
p
HTML controls eb client
W
http
server We
image bp
age

applet

client

31 October 2002 Nelson Young 7


How do applets work?
Step 4

JVM
http
server

client

Adventure.class

31 October 2002 Nelson Young 8


Applet Example:
Applet0.html

31 October 2002 Nelson Young 9


Applet Example: (Cont’d)
Applet0.java

31 October 2002 Nelson Young 10


Applet Example: (Cont’d)

31 October 2002 Nelson Young 11


Applet's Attributes
Attribute Explanation Example
Code Name of class file Code=“applet0.clas
s”
Width Width of applet Width=300
height Height of applet Height=60
Codebase Applet’s Directory Codebase=“/applet
s”
alt Alternate text if Alt=“menu applet”
applet not available

name Name of the applet Name=“appletExa


m”
Align(top,left,right,bottom) Justify the applet with Align=“right”
31 October 2002 Nelson Young 12
text
What are the
disadvantages of applets?
 Applets can’t run any local
executable programs
 Applets can’t with any host other
than the originating server
 Applets can’t read/write to local
computer’s file system

31 October 2002 Nelson Young 13


What are the
disadvantages of applets?
(Cont’d)
 Applets can’t find any information
about the local computer
 All java-created pop-up windows
carry a warning message
 Stability depends on stability of the
client’s web server
 Performance directly depend on
client’s machine

31 October 2002 Nelson Young 14


What are the advantages
of applets?
 Automatically integrated with HTML; hence,
resolved virtually all installation issues.
 Can be accessed from various platforms and
various java-enabled web browsers.
 Can provide dynamic, graphics capabilities
and visualizations
 Implemented in Java, an easy-to-learn OO
programming language

31 October 2002 Nelson Young 15


What are the advantages
of applets? (Cont’d)
 Alternative to HTML GUI design
 Safe! Because of the security built into
the core Java language and the applet
structure, you don’t have to worry about
bad code causing damage to someone’s
system
 Can be launched as a standalone web
application independent of the host web
server
31 October 2002 Nelson Young 16
What is a servlet?
 A servlet is a Java program that
executes in a special secure
environment, such as
 Inside servlet-support web servers
 Standalone servlet containers

31 October 2002 Nelson Young 17


What is a servlet? (Cont’d)
 Commonly run inside a web server,
servlets are dynamically loaded to
extend the functionality of the
server
 Functionally, servlets is an
alternative to CGI scripts for
creating dynamic web content

31 October 2002 Nelson Young 18


Common Servlet
Containers
 iPlanet (Netscape) Web Server Enterprise
Edition
 Zeus Web Server
 World Wide Web Consortium’s Jigsaw Server
 Sun’s Java Web Server
 Apache’s Web Server via JServ module
 Apache’s Tomcat Server

31 October 2002 Nelson Young 19


How do servlets works?
Step 1
 Download and install
Sun’s Java Software
Development Kit
(SDK) to allow
compilation and
interpretation of
compiled class files
 Download and install
servlet-support server

31 October 2002 Nelson Young 20


How do servlets work?
Step 2
 Write the Java source
code for the applet in
any text editor.
 Compile the source
code to obtain the
necessary bytecodes
known as “.class” for
interpreting

31 October 2002 Nelson Young 21


How do servlets work?
Step 3
 Start the servlet
engine to allow
servlets to be
dispatched

31 October 2002 Nelson Young 22


How do servlets work?
Scenario 1: Web Server with built-in servlet support
Server
Apache’
s Web Port 80
JServ Module
Server
via
JServ
Web page client

HTML controls
System Resources
image

applet

client

31 October 2002 Nelson Young 23


How do servlets work?
Scenario 2: Standalone Servlet Container
Server

Apache’s
Static Web page Tomcat Any Port
Server

Web page client


HTML
controls
image System Resources

applet

client
31 October 2002 Nelson Young 24
Servlet Life Cycle
Java Servlet-based Web Server

Main Process

Request for Servlet1


JVM

thr
e ad

Request for Servlet2 thre Servlet1


ad

thre Servlet2
ad

Request for Servlet1

31 October 2002 Nelson Young 25


Servlet Example 1:
HelloWorld.java

31 October 2002 Nelson Young 26


Servlet Example 1:
Servlet Output

31 October 2002 Nelson Young 27


Servlet Example 2:
HelloWWW.java

31 October 2002 Nelson Young 28


Servlet Example 2:
Servlet Output

31 October 2002 Nelson Young 29


What are the advantages
of servlets?
 Portability
 Portable across operating systems and across
web servers
 Power
 Harness the full power of the core Java APIs:
networking and URL access, multithreading,
image manipulation, data compression, JDBC,
object serialization, internationalization

31 October 2002 Nelson Young 30


What are the advantages
of servlets? (Cont’d)
 Efficiency & Endurance
 Memory resident, so invocation highly efficient
—no process to spawn or interpreter to invoke
 Safety
 Support safe programming since inherit Java’s
strong type safety, exception-handling
mechanism

31 October 2002 Nelson Young 31


What are the advantages
of servlets? (Cont’d)
 Elegance
 Code is clean, object-oriented, modular,
and simple (i.e.. Session tracking, cookie)
 Integration
 Tightly integrated with the server—
translate file paths, perform logging, check
authorization, and MIME type mapping

31 October 2002 Nelson Young 32


What are the
disadvantages of servlets?
 Developers MUST know JAVA
 Web Administrator will need to learn how
to install and maintain Java Servlets
 Tedious uses of out.println() statements
 Can be remedied by using Java Server Page
(JSP)

31 October 2002 Nelson Young 33


JSP Example: JSP Code

31 October 2002 Nelson Young 34


JSP Example: JSP Output

31 October 2002 Nelson Young 35


Is there a relationship
between applets and
servlets?
 Technically, NO!
 Thought to be applet that runs on the server
side - without a face
 Effective in building client/server or other
networked applications
 GelScape: image manipulation by applet (front-
end), image storage by servlet (back-end)
 LIMS: GUI design by applet (front-end),
information bridge by servlet (middle), Database
storage by JDBC (backend) --- “3 Tiers Application
Design”

31 October 2002 Nelson Young 36


Applet-Servlet Mechanism
Applet

Image Manipulation
10101010101010
10110101011010
10101010011111
11111111111111
11111000

Servlet 101010
Serialize

Text File

101010

101010

Database

31 October 2002 Nelson Young 37


Applet/Servlet Model
Server Any Port

Web page Port 80


Servlet Engine Any Port
HTML controls
Browser
image Applet

applet

System Resources

client

31 October 2002 Nelson Young 38


Applet/Servlet Example:
GelScape
Servlet-support Server

www.gelscape.org:8080/servlet/saveImage

Http Servlet Servlet Params


Text File

image
Http Post or Get
Message from applet
Applet

Object Serialization
www.gelscape.org:8080/servlet/saveImage

31 October 2002 Nelson Young 39


Summary
 Applets bring life to the traditional HTML
pages
 Servlets create dynamic web pages
 Both, either working together hand-to-
hand or not, enhance web development
by extending server functionality and at
the same time, enrich our web
experience with interactivity

31 October 2002 Nelson Young 40

You might also like