What are The Difference Between Applet and Servlet in Java

Applet and servlet are the small Java programs or applications. But, both get processed in a different environment. The basic difference between an applet and a servlet is that an applet is executed on the client-side whereas, a servlet is executed on the server-side. Both of them differ in many contexts, let us study the difference between applet and servlet with the help of comparison chart.

Comparison Chart

BASIS FOR COMPARISON
APPLET
SERVLET
Execution Applet is always executed on the client side. Servlet is always executed on the server side.
Packages import java.applet.*; import javax.servlet.*;
import java.awt.*; import java.servlet.http.*;
Lifecycle methods init(), stop(), paint(), start(), destroy(). init( ), service( ), and destroy( ).
User interface Applets use user interface classes like AWT and Swing. No User interface required.
Requirement Requires java compatible browser for execution. It processes the input from client side and generates the response in terms of the HTML page, Javascript, Applets.
Resources As it arrives at the client, it uses the resources of the client to produce graphical interface and run complex computation. It utilizes the resources of the server to process request and response of the client.
Bandwidth Utilization Applets utilize more network bandwidth as it executes on the client machine. Servlets are executed on the servers and hence require less bandwidth.
Security More prone to risk as it is on the client machine. It is under the server security.

Definition of Applet

Applet is a small Java program that is embedded in an HTML code for its execution, and it is executed on the client-side machine. Java ‘s API library contain a package called “applet”, which contains a class called “Applet”. Any applet you create should be a subclass of the class Applet, and that subclass must be declared “public” as its code will be accessed by the code that lies outside the program. Now, let us understand the creation of applet with the help of a simple example.

import java.awt.*;
import java.applet.*;
public class Hello extends Applet {
public void paint(Graphics g) {
g.drawString("Hello Applet", 20, 20);
}
}
In above code, two import statements are package “awt” and package “applet” which are required in the creation of any applet. The paint( ) method in the code is defined in the package awt, which is overridden by the applet created. As you can see the class Hello, has extended the class Applet, which is defined inside the package applet. Now, you have to save this file with the class name i.e. Hello.java. There are two methods to get the applet executed that are:

Execute applet in a Java-compatible web browser.
Execute using appletviewer which is also the fastest method of executing the applet.
The first method of executing an applet in a Java-compatible web browser requires creating an HTML program that embeds the applet created in Hello.java file.

//html code
<applet code="Hello" width=100 height=80>
</applet>
Here, the applet code, “Hello” is the name of the file in which you had created the applet. Now, save this file let say, hello.html. All you need to execute this file in a web browser is to load this HTML file in the web browser, and the applet will get executed.

The Second method for executing an applet in an appletviewer is that the commands you require to execute the applet in appletviewer are given below.

>appletviewer hello.html
There is also an another convenient method to speed up the execution. Embed the HTML code as a comment in the start of the source file Hello.java

import java.awt.*;
import java.applet.*;
/*
<applet code="Hello" width=100 height=80>
</applet>
*/
public class Hello extends Applet {
public void paint(Graphics g) {
g.drawString("Hello Applet", 20, 20);
}
}
To execute the applet you have to pass the command:

>javac Hello.java
>appletviewer Hello.java
The lifecycle method in the Applet class are, init( ), service( ), and destroy( ). The init( ) method is invoked when an applet is initialized. The start( ) method is invoked when an applet is started or restarted. The stop( ) method is invoked when an applet is terminated. The paint(Graphics) method is invoked when an applet needs to be repainted. The destroy( ) method is invoked when an applet is being destroyed.

Note:
You can notice that the applet class does not contain main( ) method.Instead, the execution of the applet begin when the name of the applet is passed to the appletviewer or the HTML file containing applet name is loaded in the web browser.

Definition of Servlet

Like Applets, Servlets are also the small Java programs that get execute on the server side. The performance issues in the platform-dependent CGI programs let to the introduction of Servlets. Servlets are platform-independent. The main purpose of a servlet is to collect the request from the client and generate the requested web page dynamically for a corresponding request and send it back to the client.

Servlets can be created using the package javax.servlet and java.servlet.http. The lifecycle methods of the servlets are init( ), service( ), and destroy( ). These methods are invoked by the server when they are required.
init( ): This method is invoked by the server when the servlet is initially loaded into the memory.
service( ): This method is invoked to process the HHTP request t which is sent by the client.
destroy( ): This method is invoked to release the resources that were allocated to the servlet.

Key Difference Between Applet and Servlet in Java
  • An applet is an application that is executed on the client machine whereas, a servlet is an application that is executed on the server machine.
  • The package used to create an applet are, import java.applet.*; and import java.awt.*; whereas, the packages used to create a servlet are, import javax.servlet.*; and import java.servlet.http.*;
  • The lifecycle methods of the Applet Class are init(), stop(), paint(), start(), destroy(). On the other hand, the lifecycle method are init( ), service( ), and destroy( ).
  • Applets use the user interface classes AWT and Swing to create the user interface whereas, a servlet does not require any user interface class as it does not create any user interface.
  • To get an applet executed on the client machine, the Java compatible web browser is required. On the other hand, the servlet requires Java enabled the web server to process the request and response of the client.
  • Applet utilizes the resources of the client machine as it executes on the client side. Servlets utilize the resources of the server as it is executed on the server side.
  • Applets face more security issues as compared to servlets.

Post a Comment

Previous Post Next Post