How can my applet or application communicate with my servlet?

Alex Chaffee



It's pretty straightforward. You can use the and classes to open a standard HTTP connection to the web server. The server then passes this information to the servlet in the normal way.

Basically, the applet pretends to be a web browser, and the servlet doesn't know the difference. As far as the servlet is concerned, the applet is just another HTTP client.

(Of course, you can write a servlet that is meant to be called only by your applet, in which case it *does* know the difference. You can also open a ServerSocket on a custom TCP port, and have your applet open a Socket connection. You must then design and implement a custom socket-level protocol to handle the communication. This is how you could write, e.g., a Chat applet communicating with a servlet. In general, a custom protocol requires more work than HTTP, but is more flexible. However, custom protocols have a harder time getting through firewalls.)

For more detail, you can see the Sun Web Server FAQ ( Questions C8 ( and C9 ( .

Also, Chad Darby has an article with source code ( on the subject.

And Netscape DevEdge Online has a similar article - Applet-to-Servlet Communication for Enterprise Applications ( skip to the "Communication Tactics" section to get to the good part.

See also:

About | Sitemap | Contact