Tutorial
Menus

Servlet - Advanced Servlet

Applet - Servlet Communication

Rating: 5.0/5 (1 vote cast)

Level   : Intermediate
Author : Arunkumar S
Download Source : echo.zip

Applet Servlet Communication Tutorial

Java applets and servlets can be used together in the design of today's multitiered web applications.
Applets provide a convenient mechanism for building powerful, dynamic interfaces to applications,
while servlets give us a highly efficient means to handle requests on a web or application server.
Sun's Application Programming Model, which describes the best practices for developing enterprise
Java applications on the Java 2 platform, recommends using applets, HTML, and JavaServer Pages
on the front end and Java servlets backed up by Enterprise JavaBeans or other components on the
back end.

Applets

Java applets are essentially Java programs that run within a web page. They're Java classes that
extend the
java.applet.applet class and are embedded by reference within an HTML page, much like an image. Combined
with HTML, they can make an interface more dynamic and powerful than with HTML alone. While some applets do
nothing more than scroll text or play animations, they can be used in an enterprise application to view or manipulate
data coming from some source on the server. For example, an applet may be used to browse and modify records in a
database or control runtime aspects of some other application running on the server.
Besides the class file defining the Java applet itself, applets can use a collection of utility classes,
either by themselves or archived into a JAR file. The applets and their class files are distributed
through standard HTTP requests and therefore can be sent across firewalls with the rest of the web
page data. Applet code is refreshed automatically each time the user revisits the hosting web site,
eliminating the concern of keeping the full application up to date on each client desktop to which it's
been distributed.

Thanks to Java's operating system agnosticism, applets can run in any browser with a Java virtual
machine (JVM). Sun's Java Plug-in software even lets you build pages that can take advantage of
the latest JVM, instead of being restricted to whatever version of the JVM your user's browser
happens to have implemented.

Since applets are extensions of the Java platform, you can reuse existing Java components when
you build at least a portion of your web application interface with applets. As we'll see in a later
example, you can use complex Java objects developed originally for server-side applications as
components of your applets. In fact, you can write Java code that can operate as either an applet or
an application.

Applets have all of the functionality of a traditional Java application, including the ability to use
advanced JFC/Swing components from Sun. Applets have the full graphical and user interface
Applets have all of the functionality of a traditional Java application, including the ability to use
advanced JFC/Swing components from Sun. Applets have the full graphical and user interface
capability of applications (though any secondary windows you create will be marked with "Warning,
Java Applet Window" notifications). But despite their similarities, there are some key differences
between applications and applets. The one that concerns us here has to do with the security
constraints imposed on applets.

Servlets

Java servlets are server-side components that are analogous in many ways to CGI programs. They
handle web requests, returning data or HTML programmatically rather than from a static file. They
can access databases, perform calculations, and communicate with other components such as
Enterprise JavaBeans. Unlike CGI programs, however, servlets are persistent - they're instantiated
once and continually handle requests (usually many simultaneous requests) for the life of the web
server. They're therefore operating at a much higher level of efficiency than CGI programs.

Servlets run within a servlet engine, usually on a web or application server. Both Netscape
Enterprise Server 4.0 and Netscape Application Server support recent releases of the Java servlet
specification. Unlike applets, servlets aren't burdened with security restrictions. Since they execute
entirely on the server, they can run with all the capabilities that the operating system allows.

Servlets can be used to create an easily accessible interface between clients, such as applets and
web browsers, and the core enterprise applications behind them. To the client, requests that go to
servlets look no different from any other web request. The client contacts a URL and is given results
back. As we'll see, the results can be a lot more than just HTML. Virtually any kind of data can be
sent and received via the HTTP protocol.

This example shows you, how to send data to a servlet from an applet and receive an answer. The servlet is an echo server, which simply sends back the string from the applet. The applet looks like this:

All precompiled and tested with Tomcat: echo.zip. For installing, extract it under tomcat/webapps/echo and add to your tomcat/conf/server.xml the following line:

<Context path="/echo" docBase="echo" reloadable="true"/>

If you have installed Tomcat on your localhost, you can call the applet like this: http://localhost:8080/echo/index.html



1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com