JSP Servlets   «Prev  Next»

Lesson 1

Servlets interact with Applets

In this module I will discuss how an applet can communicate with a servlet. When you send information from a form to a servlet, the flow is one-way: the form sends information to the servlet, and in return the servlet sends information to the browser, which replaces the form displayed.
With an applet involved, two-way communication is much simpler. After completing this module you will be able to:
  1. Design an applet that will communicate with a servlet
  2. Add code to an applet to send a GET request to a servlet
  3. Add code to an applet to read the servlet response
  4. Write a servlet that responds to an applet

Applet Servlet Communication

This module demonstrates several techniques by which applets can communicate with servlets. We are going to come at the topic from a slightly different angle than you might expect. Instead of assuming you have an applet and a servlet that need to communicate, we are going assume you have an applet that needs to talk to some entity on the server and explore why sometimes that entity should be a servlet.
Let us think about applets that need to communicate with the server. Take a look at the administration applet that manages the Java Web Server. Think about how it works, it executes on the client, but it configures the server. To do this, the applet and the server need to be in near constant communication. As another example, take a look at one of the popular chat applets. One client says something, and all the rest see it. How does that work?
They certainly do not communicate applet to applet. Instead, each applet posts its messages to a central server, and the server takes care of updating the other clients. Finally, imagine an applet that tracks the price of a set of stocks and offers continuous updates.
Question: How does the applet know the current stock prices, and, more importantly, how does it know when they change? The answer is that it talks with its server.

Java Servlets