You are here:   HomeJava Servletintroduction to ServletApplet to Servlet Communication
by Dinesh Thakur Category: introduction to Servlet

HTML exhibits high performance by taking less time to load in the browser. However, when we use HTML page for important user details, by default, all the parameters that are passed appended in the URL. This compromises with the security. On the other hand, applet takes more time to load but there is no problem with Java security. This is an advantage of this technique.

Applet is a compiled Java class that can be sent over the network. Applets are an alternative to HTML form page for developing websites. HTML form gives good performance, takes less time to load but has poor security. Whereas, Applets give poor performance, take time to load but have good security.

There are two types of Applets:

1. Untrusted Applets: It cannot interact with files and file system, so writing malicious codes is not possible. Applets are untrusted.

2. Trusted Applets: It can interact with files and file system so can write malicious codes.

                             Difference between applet and servlet




Used to develop client side web-resource program to generate static web page.

Used to develop server side web-resource program to generate dynamic web-page.

Needs browser windw or appletviewer for execution.

Needs servlet container for execution.

Applet program comes to browser window from server for execution.

Servlet program reside and execute in web resource.

The life cycle methods are init(), start(), stop() and destroy().

The life cycle methods are init(-), Service(-,-)  and destroy().


Similar to HTML-to-servlet communication we need to perform applet-to servlet communication. In HTML-to-servlet communication the browser window automatically forms request URL and automatically forms query string having form data, when submit button is clicked. In applet-to-servlet communication all these activities should be taken care of by the programmers manually or explicitly as event handling operation.

Example on Application of Applet to Servlet Communication

               Applet to Servlet Communication

Frame is a logical partition of the web page. Frame with name is called Named ,


Step 1: Prepare the deployment directory structure of web application.

Request url http://loca!host:2020/ AtoSApp/Main.html

Step 2: Develop the source code of above servlet program or web Application.

Source Code
import javax.servlet.*;
import javax.servlet.http.*;
import Java.util.*;
public class wishsrv extends HttpServlet
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException , IOException
//general settings{);
setContentType("text/html") ;
//read form data
String name=req.getParameter("uname") i
//generate wish message
Calendar cl=Calendar.getlnstance();
int h=cl.get(Calendar.HOUR_OF_DAY);
if (h<=12)
pw. println ("Good Morning :"+name) i
pw.println("Good Afternoon: "+name);
pw.println("Good Evening :"+name);
pw.println("Good Night :"+name);
//close stream obj
pw.close() i
} / /class
<> javac
Configure MyServletprogram with /testurl url pattern and also configure Main.html as welcome file.
<frameset rows = "40% , *">
<frame name = "f1" SYC = "Form.html">
<frame =  "f2" />
// MyApplet. Java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyApplet extends Applet implements ActionListener
Button b;
TextField tfl;
Label l1;
public void init ()
l1 = new Label ("User name :");
add(l1) ;
tfl = new TextField(10);
add(tf1) ;
b=new Button("Send");
b.addActionListener(this) ;
add(b) ;
public void actionPerformed(ActionEvent ae)
//read text value
String name=tfl.getText().replace();
//frame query String
String qrystr=(“?uname=”+name) ;
//frame request url having query String
String url=(“http://localhost:2020/AtoSApp/testurl”+qrystr);
//create URL class object
URL requrl = new URL (ur1);
//getAppletContext obj
AppletContext apc=getAppletContext();
}catch(Exception ee)




<applet code= "MyApplet.class" width= "500" height= "500">


Step 3: Compile the source files of all servlet programs.

Step 4: Configure all the four servlet programs in web.xml file having four different url patterns.

Step 5: Start the server (Tomcat).

Step 6: Deploy the web application.

Copy AutoSApp folder Tomcat_home \ webapps folder.

Step 7: Test the web application.

Open browser window type this url-http://localhost:2020/ AtoSApp/ Main.html

What's New and Popular

Search Content

Advance Courses

Basic Courses

Advertise with Us