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

 

APPLET

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 ,

Frame.

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

 
MyApplet.java
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import Java.util.*;
public class wishsrv extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException , IOException
{
//general settings
PrintWriter.pw=res.getWriter{);
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
elseif(h<=16}
pw.println("Good Afternoon: "+name);
elseif(h<=20}
pw.println("Good Evening :"+name);
else
pw.println("Good Night :"+name);
//close stream obj
pw.close() i
}//doGet
} / /class
<> javac MyServlet.java
web.xml
Configure MyServletprogram with /testurl url pattern and also configure Main.html as welcome file.
<web-app>
        <servlet>
            <servlet-name>abc</servlet-name>
            <servlet-class>MyServlet</servlet-class>
        </servlet>
          <servlet-mapping>
             <servlet-name>abc</servlet-name>
             <url-pattern>/testurl</url-pattren>
          </servlet-mapping>
          <welcome-file-list>
              <welcome-file>Main.html</welcome-file>
          </welcome-file-list>
</web-app>
 
Main.html
<frameset rows = "40% , *">
<frame name = "f1" SYC = "Form.html">
<frame =  "f2" />
</frameset>
 
MyApplet.java
 
// MyApplet. Java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
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)
{
try{
//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)
{}
{}

 

Form.html

 

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

</applet>

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