Java Applets
1. What is an Applet?
An applet is a small Java program that is embedded in an HTML page, downloaded from a web server, and executed inside the client's browser (or appletviewer) by the Java Plug-in / JVM.
SERVER CLIENT (browser)
+------------------+ HTTP (html) +---------------------------+
| hello.html | ---------------> | browser parses <applet> |
| <applet code= | | | |
| Hello.class> | HTTP (jar/ | v |
+------------------+ class files) | Java Plug-in JVM |
| Hello.class | <--------------- | loads, verifies, runs |
+----------------+ download | applet -> displays GUI |
+---------------------------+
Key properties: applets have no main(); they run in a sandbox with heavy security restrictions; the class file is downloaded on demand each time the page is loaded (always up to date).
2. Applet Life Cycle
init() -> called once when the applet is loaded; read parameters, set up GUI
start() -> after init() and every time the page is revisited / applet resumed
paint() -> drawing; called whenever the applet needs to redraw itself
stop() -> when the user leaves the page / applet is paused
destroy() -> called once before the applet is unloaded; release resources
Embedding in HTML:
<applet code="HelloApplet.class" width="300" height="100">
<param name="msg" value="Hi from the server">
</applet>
(Later standard: the object tag with classid, or <embed>; modern replacement: Java Web Start / HTML5+JS.)
3. Applet Class Hierarchy and AWT
java.lang.Object
-> java.awt.Component -> Container -> Panel -> Applet -> JApplet (Swing)
Applet is a Panel, so it is a container: you can add() AWT/Swing components (buttons, text fields, labels) and receive events (ActionListener etc.).
4. Applet Security Model (Sandbox)
| Allowed | Prohibited (unsigned applet) |
|---|---|
| Display GUI, images, audio from its own host | Read/write local file system |
| Network connections only to its origin server | Open sockets to other hosts |
| Play sounds, animation, user interaction inside browser | Execute local programs |
| Read system properties (non-critical) | Load native libraries / call native methods |
| Access its own thread group | Make network connections except to its origin host |
A digitally signed applet (with a certificate the user accepts) can escape the sandbox; appletviewer offers restricted run outside the browser.
5. Passing Parameters
<param name="font" value="bold"> → inside the applet:
String f = getParameter("font"); // returns null if absent
showStatus("loading..."); // write to the browser status bar
6. Animation and Graphics
paint(Graphics g)withg.drawLine/g.drawString/g.setColor/g.fillRect.- Override
update()to avoid flicker, or use double buffering (draw to an offscreen Image, then blit). - Use
Thread+repaint()in a loop for animation;AudioClip.play()for sound.
7. Advantages and Limitations
Advantages
- Cross-platform GUI inside any Java-enabled browser; automatic distribution/update from the server.
- Secure (sandbox) by design; can access rich Java libraries (networking, JDBC to its origin host).
- Lighter than installing a standalone application.
Limitations (why applets are dead today)
- Requires JVM plug-in in the browser; slow start-up (download + JVM init).
- Sandboxed — cannot freely access client resources.
- No support in modern browsers (applet support removed ~2015-2017); replaced by HTML5 + JavaScript/WebGL or Java Web Start / JavaFX.
- Difficult SEO/caching; bandwidth for class downloads.
8. Exam Points
- Explain the applet life cycle with methods and diagram — 5-7 marks (very frequent).
- Write a simple applet program + HTML embedding — 10 marks.
- Explain the applet security model / sandbox — 5 marks.
- Differentiate applet and application (main() vs life cycle, sandbox, deployment) — 5 marks.
- How parameters are passed to an applet — 3-5 marks.