
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Applets and Servlets in Java.
In Java, both Applets and Servlets are the programs or applications that run in a Java environment. Applets are designed to provide interactive features that can be embedded into web pages and allow users to engage with content directly through their web browsers.
On the other hand, Servlets operate on the server side, they handle requests and responses, which is most important for generating dynamic content in web applications. The main difference in both programs is that their processing is done in different environments.
Difference between Applets and Servlets
Some of the major differences between Applets and Servlets are as follows.
Key | Applets | Servlets |
---|---|---|
Execution | Applets are executed on client-side i.e applet runs within a Web browser on the client machine. | Servlets on the other hand executed on the server side i.e servlet runs on the web page on the server. |
Parent packages | The parent package of Applet includes java.applet.* and java.awt.* | Parent package of Servlet includes javax.servlet.* and java.servlet.http.* |
Methods | Important methods of applet includes init(), stop(), paint(), start(), destroy(). | The lifecycle methods of servlet are init( ), service( ), and destroy( ). |
User interface | For the execution of the applet, a user interface is required such as AWT or swing. | No such interface is required for the execution of the servlet. |
Required Bandwidth | The applet requires a user interface on the client machine for execution so it requires more bandwidth. | On the other hand, Servlets are executed on the servers and hence require less bandwidth. |
Secure | Applets are more prone to risk as execution is on the client machine. | Servlets are more secure as execution is under server security. |
Example of applet vs servlet
For brief understanding, we have provided the code difference between applet and servlet. Below is the program that shows how we can use applet in java.
AppletDemo.java
import java.applet.Applet; import java.awt.Graphics; public class AppletDemo extends Applet { // Overriding paint() method @Override public void paint(Graphics g){ g.drawString("AppletDemo", 20, 20); } }
Output
AppletDemo
Example
Below is the program that shows how we can use servlet in Java.
ServletDemo.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletDemo extends HttpServlet { private String message; public void init() throws ServletException{ // Do required initialization message = "Servlet Demo"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(message); } }
Output
Servlet Demo