DEV Community

Sudhakar V
Sudhakar V

Posted on • Edited on

Applets vs Servlets in Java

Applets and Servlets are both Java technologies, but they serve very different purposes:


Applets

What is an Applet?

  • A Java program that runs in a web browser.
  • Meant for client-side execution.
  • Requires a Java plugin in the browser (now deprecated and unsupported by most browsers).

Characteristics:

Feature Description
Execution Runs on the client machine (browser).
GUI Support Yes (Swing or AWT).
Use Case Old-school interactive web UIs.
Security Runs in a sandbox (restricted).
Obsolete? Yes. No longer supported by browsers.

Example:

import java.applet.Applet;
import java.awt.Graphics;

public class HelloApplet extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello from Applet!", 20, 20);
    }
}
Enter fullscreen mode Exit fullscreen mode

Servlets

What is a Servlet?

  • A Java class that runs on a server and handles HTTP requests and responses.
  • Used to build web applications (server-side logic).
  • Forms the foundation for Spring MVC, JSP, and Java EE web apps.

Characteristics:

Feature Description
Execution Runs on the server (inside servlet container like Tomcat).
GUI Support No GUI (sends HTML or data as response).
Use Case Handling requests, processing forms, serving data.
Modern Usage Backbone of Java web development.

Example:

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

public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<h1>Hello from Servlet!</h1>");
    }
}
Enter fullscreen mode Exit fullscreen mode

Applet vs Servlet Comparison Table

Feature Applet Servlet
Runs On Client (Browser) Server (Web container)
Purpose GUI applications in browsers Handle web requests/responses
GUI Support Yes (AWT/Swing) No (Returns HTML or data)
HTML Interaction Embedded in HTML pages Processes requests from HTML forms
Browser Support Deprecated Widely supported
Security Runs in restricted sandbox Server-side security (authentication etc.)
Modern Usage Obsolete Still heavily used (Spring Boot, JSP)

Conclusion

  • Applets are outdated and no longer used.
  • Servlets are foundational for modern Java web development (including Spring Boot).

Top comments (0)