Home Java Tutorial Difference Between Applet and Servlet in Java

Difference Between Applet and Servlet in Java

by anupmaurya
3 minutes read

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.

BASIS FOR COMPARISONDIFFERENCES
ExecutionAn applet is an application that is executed on the client machine whereas, a servlet is an application that is executed on the server machine.
PackagesThe 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.*;
Lifecycle methodsThe lifecycle methods of the Applet Class are init(), stop(), paint(), start(), destroy(). On the other hand, the lifecycle method are init( ), service( ), and destroy( ).
User interfaceApplets 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.
RequirementTo 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.
ResourcesApplet 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.
SecurityApplets face more security issues as compared to servlets.
Difference Between Applet and Servlet in Java

Let have a looks on Applets and Servlet Example.

Creating “Hands on Applet” Applet.

// A "Hands on Applet" Applet 
// Save file as HandsonApplet.java 
  
import java.applet.Applet; 
import java.awt.Graphics; 
  
// HandsonApplet class extends Applet 
public class HandsonApplet extends Applet { 
  
    // Overriding paint() method 
    @Override
    public void paint(Graphics g) 
    { 
        g.drawString("Hands on Applet", 20, 20); 
    } 
}

Creating “Hands on Servlet” Servlet.

// Import required java libraries 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
  
// Extend HttpServlet class 
public class HandsonServlet extends HttpServlet { 
  
    private String message; 
  
    public void init() throws ServletException 
    { 
        // Do required initialization 
        message = "Hands on Servlets"; 
    } 
  
    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response) 
        throws ServletException, IOException 
    { 
  
        // Set response content type 
        response.setContentType("text/html"); 
  
        // Actual logic goes here. 
        PrintWriter out = response.getWriter(); 
        out.println("<h1>" + message + "</h1>"); 
    } 
  
    public void destroy() 
    { 
        // do nothing. 
    } 
}

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.