Monday, April 9, 2012

Invoking Servlets from Applets




INVOKING SERVLETS FROM APPLETS
 AIM:

      To write a java program that invokes servlet from applet.

ALGORITHM:
1.      Create the java program with the following
a)   Define the class Myapplet which extends the property of the class Applet and implements the interface ActionListener.
b)  Define the objects for Button and add the button in the init() method of Applet class
c)  Make the button to listen the action by using the method addActionListener().
d)  Set the URL of the servlet program by using the object of the class URL.
e)  Define the object for AppletContext in order to display the output of the servlet on new browser window.

2.      Create HTML file that contains the applet tag and pass the class name to that applet code.
3.      Create the simple servlet program that contains any response message
4.      Run the HTML file that contains the corresponding applet code.
5.      Click the button on the applet window in order to invoke the servlet program.

 

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=blue><h2>Server Response</h2>");
  pw.println("<h3>Current Date and Time From Server:</h3>");
  pw.println("<b>"+d+"</b></body></html>");
 }
}


web.xml:

<web-app>  
    <servlet>
        <servlet-name>Applet</servlet-name>
        <servlet-class>MyServer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Applet</servlet-name>
        <url-pattern>/MyServer</url-pattern>
    </servlet-mapping>
</web-app>


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/servlets3/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>












Thanks to : http://sreebrothers.blogspot.in

15 comments:

  1. sir where is the output for this program??????

    ReplyDelete
    Replies
    1. during execution is shows this error then what can i do for this error ?

      MyServer.java:3: error: package javax.servlet does not exist
      import javax.servlet.*;

      Delete
    2. If u set the environment variables correctly, you wont get this error msg.

      Delete
  2. hi admin i ma getting this error HTTP Status 404 - /servlets3/
    type Status report
    message /servlets3/
    description The requested resource is not available.
    pleas help machi any way nice info

    ReplyDelete
    Replies
    1. if you give the url correctly in your program, u wont get this error msg. it is case sensitive too..

      Delete
  3. what is that servlets3 in "http://localhost:8080/servlets3/MyServer" ??

    ReplyDelete
    Replies
    1. servlets is the folder created for this project in tomcat.

      Delete
    2. servlets3 is the folder created for this project in tomcat.

      Delete
  4. servlets is the folder created for this project in tomcat.

    ReplyDelete
  5. where is output this program....

    ReplyDelete
  6. how to run this in netbeans

    ReplyDelete
  7. Post your output and by the way why are you using web.xml? no use for that !!! #Lol #Ani

    ReplyDelete