Tuesday, January 29, 2013

INVOKING SERVLETS FROM APPLETS


                                     INVOKING SERVLETS FROM APPLETS
AIM:

      To write a java program that invokes Servlet from applet.

ALGORITHM:
1.   Step 1:Create the java program with the following
a)        Step 2: Define the class Myapplet which extends the property of the class Applet and implements the interface ActionListener.
b)        Step 3:Define the objects for Button and add the button in the init() method of Applet class
c)          Step 4: Make the button to listen the action by using the method addActionListener().
d)         Step 5: Set the URL of the servlet program by using the object of the class URL.
e)         Step 6: Define the object for AppletContext in order to display the output of the servlet on new browser window.
 Step 7: Create HTML file that contains the applet tag and pass the class name to that applet code.
3.   Step 8:Create the simple servlet program that contains any response message
4.   Step9: Run the HTML file that contains the corresponding applet code.
5.    Step 10: Click the button on the applet window in order to invoke the servlet program.
       
PROGRAM:
AppletClient.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class AppletClient extends Applet implements ActionListener
{
 public void init()
 {
  Label la=new Label("INVOKING SERVLET FROM APPLET");
  la.setFont(new Font("Courier",Font.BOLD,15));
  la.setForeground(Color.blue);
  add(la);
  Button b1=new Button("Click Here To Display Date Information From Server");
  b1.setBackground(Color.black);
  b1.setForeground(Color.white);
  add(b1);
  b1.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae)
 {
  try
  {
   AppletContext ac=getAppletContext();
   URL url = new URL("http://localhost:8080/servlets-examples/servlet/MyServer");
   ac.showDocument(url);
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}

AppletClient.html

<html>
<head>
<title>Invoking Servlets From Applet</title>
</head>
<body bgcolor="violet">
<applet code="AppletClient.class" width="400" height="200">
</applet>
</body>
</html>



MyServer.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
public class MyServer extends GenericServlet
{
 public void service(ServletRequest req,ServletResponse res)throws  ServletException,IOException
 {
  PrintWriter pw=res.getWriter();
  Date d=new Date();
  pw.println("<html><body bgcolor=violet><h2>Srever Response</h2>");
  pw.println("<h3>Current Date and Time From Server:</h3>");
  pw.println("<b>"+d+"</b></body></html>");
 }
}
RESULT:
              Thus the program to invoke Servlets from Applets is written and output is verified.


RUN :




No comments:

Post a Comment

Leave the comments