0% found this document useful (0 votes)
10 views14 pages

Understanding Java Applets

Java applets are programs that run in a web browser. When a web page containing an applet tag is loaded, the browser downloads the applet code from the web server and executes it. Applets follow a lifecycle of init(), start(), stop(), and destroy() methods. For security, applets have restrictions on accessing files or making network connections except back to the server they were downloaded from.

Uploaded by

Monisha Ab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Understanding Java Applets

Java applets are programs that run in a web browser. When a web page containing an applet tag is loaded, the browser downloads the applet code from the web server and executes it. Applets follow a lifecycle of init(), start(), stop(), and destroy() methods. For security, applets have restrictions on accessing files or making network connections except back to the server they were downloaded from.

Uploaded by

Monisha Ab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Java applets

11/29/22 1
Introduction
Java applets are one of three kinds of Java
programs:
 An application is a standalone program that can be
invoked from the command line.
 An applet is a program that runs in the context of a
browser session.
 A servlet is a program that is invoked on a server
program, and it runs in the context of a web server
process.

11/29/22 2
Applets, web page, client, server

 Applets are programs stored on a web server,


similar to web pages.
 When an applet is referred to in a web page
that has been fetched and processed by a
browser, the browser generates a request to
fetch (or download) the applet program, then
executes the applet program in the browser’s
execution context on the client host.

11/29/22 3
Applets, web page, client, server
server ho st
bro w ser ho st
we b se rve r
browse r
re qe u s t for
m yW e bPage .h tm l

m yW e bPage .h tm l
m yW e bPage .h tm l He lloW [Link]
... re qu e st for
< apple t code = He lloW [Link] </apple t> He lloW orldclass
...
He lloW [Link] He lloW [Link] s

11/29/22 4
Applet Execution - 1

 An applet program is a written as a subclass


of the [Link] class or the
[Link] class.
 There is no main() method in an Applet.
 An applet uses AWT for graphics, or JApplet,
a subclass of [Link].

11/29/22 5
Applet Execution - 2
 Life Cycle of an Applet:
 init: This method is intended for whatever initialization
is needed for an applet.
 start: This method is automatically called after init
method. It is also called whenever user returns to the
page containing the applet after visiting other pages.
 stop: This method is automatically called whenever the
user moves away from the page containing applets.
This method can be used to stop an animation.
 destroy: This method is only called when the browser
shuts down normally.

 Ref: [Link] /

11/29/22 6
Applet Execution - 3
 The applet is running and rendered on
the web page.
 Every Applet needs to implement one of
more of the init(), the start( ) and the
paint( ) methods.
 At the end of the execution, the stop( )
method is invoked, followed by the
destroy( ) method to deallocate the
applet’s resources.

11/29/22 7
Applet Security

For security reasons, applets that are loaded over


the network have several restrictions.
 an applet cannot ordinarily read or write files

on the computer that it's executing on.


 an applet cannot make network connections

except to the host that it came from.


Ref: [Link]

11/29/22 8
HTML tags for applets - 1
<APPLET
// the beginning of the HTML applet code
CODE="[Link]"
// the actual name of the applet (usually a 'class' file)
CODEBASE="demos/"
// the location of the applet (relative as here, or a full URL)
NAME=“SWE622"
// the name of the instance of the applet on this page
WIDTH="100"
// the physical width of the applet on the page
HEIGHT="50"
// the physical height of the applet on the page
ALIGN="Top"
// align the applet within its page space (top, bottom, center)

11/29/22 9
HTML tags for applets - 2
<APPLET CODE=“[Link]" CODEBASE="example/"
WIDTH=460 HEIGHT=160
NAME="buddy" >
<PARAM NAME="imageSource" VALUE="images/Beans">
<PARAM NAME="backgroundColor" VALUE="0xc0c0c0">
<PARAM NAME="endImage" VALUE=10>
</APPLET>

11/29/22 10
The HelloWorld Applet
<HTML> public void paint(Graphics g) {
<BODY> final int FONT_SIZE = 42;
<APPLET code=[Link] width=900 height=300> Font font = new Font("Serif",
</APPLET> [Link], FONT_SIZE);
</BODY> // set font, and color and display message
</HTML>
// on the screen at position 250,150
[Link](font);
// applet to display a message in a window
[Link]([Link]);
import [Link].*;
// The message in the next line is the one
import [Link].*; // you will see
[Link]("Hello,
public class hello extends Applet { world!",250,150);
public void init( ) {
} // end of paint()
setBackground([Link]);
} // end of init()
} // end of hello

11/29/22 11
Advanced Applets
 You can use threads in an applet.
 You can make socket/RMI calls (hw_#3) in an applet, subject to the security
constraints.
Se r ve r ho s t C l i e nt ho st
H TTP s e rv e r bro ws e r

ap p le t d o w n lo ad
s e r v er Y allo w e d
c o n n ec tio n r eq u e s t a pple t

f o r b id d en
H ost X
c o n n e c tio n r eq u e s t
s e r v er Z

11/29/22 12
Proxy server
A proxy server (Y) can be used to circumvent the security
constraints.
Se r ve r ho s t C l i e nt ho s t
H TTP s e rv e r bro ws e r

ap p let d o w n lo ad
s er v e r Y
c o n n ec tio n r eq u es t a pple t

H ost X
c o n n ec tio n r eq u es t
s e r v er Z

11/29/22 13
Summary
 An applet is a Java class
 Its code is downloaded from a web server
 It runs in the browser’s environment on the client host
 It is invoked by a browser when it scans a web page and
encounters a class specified with the APPLET tag
 For security reason, the execution of an applet is
normally subject to restrictions:
 applets cannot access files in the file system on the client host
 Applets cannot make network connection exception to the
server host from which it originated

11/29/22 14

Common questions

Powered by AI

Threads in advanced Java applets are used to perform background tasks, ensuring that the user interface remains responsive. While applets can use threads, their implementation must adhere to security constraints. These constraints include restricted network access and limited system interaction, which control what a thread can perform once executed. Applets, being restricted from interacting directly with the client's file system or making unauthorized network connections, may use threads for animations or computations that do not violate these guidelines . Utilizing a proxy server can be an approach to manage more complex interactions under this security model, as it permits safe handling of external resources .

HTML tags are essential for deploying and embedding Java applets on web pages, providing a bridge between web content and Java functionality. The <APPLET> tag is used to specify the Java class files, define parameters, and set the applet’s dimensions and alignment on the web page . Specific parameters like CODEBASE for the location, WIDTH and HEIGHT for dimensions, and additional <PARAM> tags can be used to customize the applet’s appearance and behavior on the page . This integration allows applets to be seamlessly included within web content, making them interactive and visually coherent with other web elements.

The init(), start(), stop(), and destroy() methods collectively define essential stages in an applet’s behavior based on user interactions. init() is called once to initialize the applet, establishing its initial setup . start() is invoked after init() and each time the user revisits the page hosting the applet, allowing for starting or resuming operations like animations . stop() is triggered when the user navigates away, here used to pause processes or free temporary resources . destroy() is called when the applet is about to be removed from memory, usually upon browser shutdown, to release persistent resources and finalize actions . These methods map directly to user navigation and session management, ensuring efficient and responsive applet lifecycle handling.

Java applets face specific security restrictions that limit their functionality compared to standalone Java programs. These restrictions are in place because applets are often downloaded over a network and run in a client’s browser, posing security risks. For instance, applets generally cannot read or write local files on the client’s computer to protect user privacy and security . Additionally, applets are restricted in their ability to make network connections; they can generally only connect to the server from which they were downloaded, preventing unauthorized communication with other servers . These restrictions ensure that applets cannot compromise the client system, but they also limit the applet's capabilities, such as data access and network communications, compared to other Java programs.

When implementing an applet’s graphical user interface using AWT or Swing, several components must be addressed. Using AWT, applets can set properties such as the background color and draw on their window with graphics objects in methods like paint(). For instance, in the HelloWorld applet example, the background color is defined, and the drawString method is used to display text . Swing offers more sophisticated components like JApplet, allowing for more comprehensive GUI designs. However, both AWT and Swing require an understanding of component interaction, layout management, and event handling to effectively develop an interactive user interface within an applet environment .

Java applications, applets, and servlets differ primarily in their execution contexts and environments. A Java application runs as a standalone program, typically initiated from the command line, and operates independently of web browsers or servers . On the other hand, a Java applet runs in the context of a web browser and is embedded in a web page. It is invoked when a browser reads a web page containing the applet reference and executes within the browser’s execution context on the client host . A servlet, however, is executed on a web server and is invoked in the context of a web server processing client requests . Each has different security restrictions and use cases, with applets having significant network access restrictions .

The lifecycle of a Java applet involves specific methods that are not present in typical Java programs. It includes methods like init(), start(), stop(), and destroy() which manage the applet's lifecycle from initialization to termination. init() is used for initialization, start() is called after init and whenever the user returns to the page, stop() is called when moving away, and destroy() is invoked when the browser shuts down . Unlike typical Java programs that start execution from a main() method, applets rely on these lifecycle methods to manage their state and interaction within web pages .

The security model for Java applets imposes significant limitations to protect client systems. Applets cannot read or write files to the client machine’s file system, preventing unauthorized access to sensitive data or alteration of local files . Network access is similarly restricted; applets can only establish network connections with the server from which they originated to avoid potentially malicious connections to other servers outside this scope . These restrictions are crucial to maintaining the security and integrity of client systems when executing remotely downloaded applets in browser environments.

A proxy server might be used in conjunction with Java applets to circumvent certain security restrictions that applets face, particularly those regarding network communication. By relaying requests through a proxy server, the applet can communicate more broadly without directly breaching security constraints like connecting solely to its origin server . The security implications include potentially exposing the proxy server itself to vulnerabilities, as it must filter and manage the additional network traffic safely. This setup introduces an architectural complexity that requires balanced security measures to maintain control over allowed connections and data integrity .

Traditionally, Java applets have been integrated into web pages using the <APPLET> HTML tag, specifying parameters like CODEBASE, WIDTH, HEIGHT, and others for presentation and behavior . These tags ensure the applet is properly embedded and displayed on web pages alongside other content. However, the evolution of web technology, particularly the rise of HTML5 and more modern, secure scripting alternatives like JavaScript, has affected applet usage. Increasing browser restrictions and the phasing out of NPAPI support have led to declining applet deployment, as modern web frameworks offer more seamless and less restrictive methods to achieve similar functionalities without plugin reliance .

You might also like