contador javascript Skip to content
Contact :

Difference between Applet and Servlet in Java

Applet and servlet are the small programs or applications of Java. But, both are processed in a different environment. The basic difference between an applet and a servlet is that a applet it runs on the client side while a servlet It runs on the server side. Both differ in many contexts, let's study the difference between applet and servlet with the help of the comparison table.

Comparative graph

Basis for comparisonAppletServlet
Execution The applet always runs on the client side. Servlet always runs on the server side.
packages import java.applet. *; import java.awt. *; import javax.servlet. *; import java.servlet.http. *;
Life cycle methods init (), stop (), paint (), start (), destroy (). init (), service (), and destroy ().
User interface Applets use user interface classes such as AWT and Swing. No user interface required.
Requirement Requires java compatible browser for execution. It processes the entry from the client side and generates the response in terms of the HTML page, Javascript, Applets.
Resources As it reaches the client, it uses the client's resources to produce a graphical interface and execute complex calculations. Use the server resources to process the client's request and response.
Bandwidth Usage Applets use more network bandwidth as they run on the client machine. Servlets run on servers and therefore require less bandwidth.
Security More prone to risk since it is in the client machine. It is under server security.

Applet definition

Applet is a small Java program that is embedded in an HTML code for execution, and runs on the client side machine. The Java API library contains a package called "applet", which contains a class called "Applet". Any applet that you create must be a subclass of the Applet class, and that subclass must be declared "public", since the code that is outside the program will have access to your code. Now, let's understand the creation of an applet with the help of a simple example.

 import java.awt. *; importar java.applet. *; la clase pblica Hello ampla el applet {public void paint (Graphics g) {g.drawString ("Hello Applet", 20, 20); }} 

In the previous code, two import instructions are the "awt" package and the "applet" package that are required in the creation of any applet. The paint () method in the code is defined in the awt package, which is replaced by the applet created. As you can see, the Hello class has extended the Applet class, which is defined within the applet package. Now, you must save this file with the class name, that is, There are two methods to run the applet that are:

  • Run the applet in a Java compatible web browser.
  • Run using appletviewer, which is also the fastest method to run the applet.

The first method to run an applet in a Java-compatible web browser requires creating an HTML program that embeds the applet created in the file.

 //cdigo HTML 

Here, the applet code, "Hello" is the name of the file in which you created the applet. Now, save this file, say, hello.html. All you need to run this file in a web browser is to load this HTML file into the web browser, and the applet will run.

The second method to run an applet in an appletviewer is that the commands you need to run the applet in appletviewer are indicated below.

 > appletviewer hello.html 

There is also another convenient method to speed up execution. Embed the HTML code as a comment at the beginning of the source file

 import java.awt. *; importar java.applet. *; / * * / public class Hello extiende Applet {public void paint (Graphics g) {g.drawString ("Hello Applet", 20, 20); }} 

To run the applet you have to pass the command:

 > javac> appletviewer 

The life cycle 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 method (Graphics) is invoked when an applet needs to be repainted. The destroy () method is invoked when an applet is destroyed.

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

Servlet definition

Like applets, servlets are also small Java programs that run on the server side. Performance problems in platform-dependent CGI programs allow the introduction of Servlets. Servlets are independent of the platform. The main purpose of a servlet is to collect the client's request and dynamically generate the requested web page for a corresponding request and send it back to the client.