Difference between Applets and Servlets

Difference between Applets and Servlets in Java

Applets and  Servlets Difference

  • The Key Difference between Applets and Servlets is that Applet is run on the client-side whereas, a Servlet is run on the server-side. In other words, Applet is client-side programming whereas the servlet is server-side programming.
  • Applet is for the front end and servlet is for the backend.
Applet and Servlet
Applet and Servlet

Applets vs Servlets Comparison Chart

Applets Servlets
Applet is part of Core Java. Servlet is part of Advance Java.
Applets may have a graphical user interface(GUI). Servlets have no graphical user interface.
Applets are extended to the web browser. Servlets are extended to the web servers.
Applets are the program on the client-side that runs on the web browser. Servlets are the program on the server-side which runs on the webserver.
An Applet can use the user interface classes like AWT or Swing. Servlet does not have a user interface.
Applets are treated as untrusted and they have limited permission to run in the client browser Servlets can be treated as Server-side applets. It runs in a servlet container which is deployed in the webserver.
An applet is downloaded into the client’s machine and run on the client’s browser. Servlet runs on the server and transfers the results back to the client when it is done.
Using applets, the entire code of the applet has to be transferred to the client. Therefore it consumes more network bandwidth than servlet. While Servlets, which transfers only the results to the client.
Applets can make request to servlets. The servlets are intended to respond to the applets or HTML program.
Lifecycle methods of Applet init(), stop(), paint(), start(), destroy(). Lifecycle methods of servlets init( ), service( ), and destroy( ).
The packages used for Applets are:

import java.applet.*;
import java.awt.*;

The packages used for Servlets are The

import javax.servlet.*;
import java.servlet.http.*;




Coding Example of Applet vs Servlet

  • Applet Example

import java.applet.Applet;
import java.awt.Graphics;
public class TestApplet extends Applet {
   @Override
   public void Test(Graphics g){
      g.drawString("TestApplet", 20, 20);
   }
}

Output: TestApplet

  • Servlet Example

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletTest extends HttpServlet {
   private String message;
   public void init() throws ServletException{
      message = "ServletTest";
   }
   public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println(message);
   }
}

Output: ServletTest




More Difference