Applet and Servlet in Java are small Java programs or applications. The basic difference between the two is that, execution of Applet is done at client-side while the execution of Servlet is done at sever-side. Both the terms differ in several contexts which will be discussed in the article.
Firstly, the basic understanding of Applet and Servlet is given, later, the difference between the two is briefly discussed.
Also Read:- Advantages of Java Over Conventional Languages
What Is An Applet?
Applet is a small Java-based program that is embedded in the webpage to generate dynamic content. Applet is generally embedded with HTML in a website. It runs on a client machine. A webpage containing Applet can easily be viewed on a browser that has Java enabled.
The Applet is executed using Java Virtual Machine (JVM). The Applet is executed only when the Applet is launched by a user within the browser.Appletis platform independent as it runs on Java Virtual Machine (JVM). Furthermore, Java loads faster as it is cached by majority of browsers.
Security of Java Applet is determined by the two variations of Applets known as signed Applet and unsigned Applet. UnsignedApplet is the one that have restrictions such as access to local file system is not possible. Access to the Applet download site is the only possible thing on the web.
SignedAppletcontains a signature that needs to be verified before it becomes equivalent to a standalone application once its signature is verified.
The API library of Java contain ‘Applet’ named package, which has a class named “applet”. Applet is the superclass of all the Applets created by developer. The applet subclass must be declared “public” as its code will be accessed by the code that lies outside the program.Unlike the Other Java Programs, Applets don`t have a main() method.The packages used for Applets in Java arejava.appletandjava.awt.
init( ), start( ), stop( ), paint( ) and destroy( ) are the lifecycle methods in the Applet class.
init( ):The init( ) method is called during the initialization of the Applet.
start( ):When an Applet is starts or restarts, it is then the start( ) method is called. stop( ):The stop( ) method is called at the time of termination of an Applet.
paint(Graphics):The paint(Graphics) method is called when it is required for an Applet to be repainted.
destroy():The destroy( ) method is called when an Applet is being destroyed.
What Is A Servlet?
Servlet is also a small Java program similar to an Applet but runs on a server-side and is used for the purpose of extending the functionalities of a server. Servlets mostly extends the functionalities of the server that hosts an HTTP model i.e. a request-response model.
A web container is used for the deployment of a Java Servlet.Dynamic content can be added to a web browser using the Java platform with the help of a Java Servlet.
The major role of a servlet is the collection of a request from the client and dynamically generating the requested web page for a corresponding request and sending it back to the client.
The packages used for creating a Servlet arejavax.servlet and java.servlet.http.
Init( ), service( ), and destroy( ) are the lifecycle methods of Servlet.
init( ): The server invokes the init( ) method when the servlet is initially loaded into the memory.
service( ): The server invokes the service( ) method when the processing of the HTTP request which is sent by the client is required.
destroy( ): The server invokes the service( ) method when the resources that were allocated to the servlet are required to be released.
Major Differences Between Applet And Servlet In Java
- Execution
An Applet is a Java program that runs on a client machine while a Servlet is a Java program that runs on a server machine.
- Lifecycle Methods
The lifecycle methods in an Applet are init(), stop(), paint(), start(), destroy() whereas the lifecycle method for a Servlet are init( ), service( ), and destroy( ).
- Packages
Packages used for creating an Applet are import java.Applet.*; and import java.awt.*;
Packages used for creating a Servlet are import javax.servlet.*; and import java.servlet.http.*;
- Requirement
For an Applet to be executed, a Java compatible browser is required.
On the other hand, for a Servlet to be executed, a Java enabled browser is required for the purpose of processing the request from client side and generating the response in terms of the HTML page, JavaScript, Applets.
- User Interface
To create a user interface an Applet requires user interface classes such as AWT and Swing.
On the other hand, a Servlet does not create any user interface hence there is no requirement of any user interface class.
- Security
Being present on the client machine, Applets are more prone to risks while Servlets are less prone to risks are they server secured.
- Bandwidth Utilization
Applets require more network bandwidth as it runs on a client machine while Servlets require less bandwidth.
- Resources
An Applet uses the resources of a client for producing graphical interface and run complex computation because it executes on a client machine. On the other hand, Servlet uses server’s resources for processing request and response of the client.
These are the basic differences between an Applet and a Servlet in Java.