INVOKING SERVLETS FROM
APPLETS
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>
sir where is the output for this program??????
ReplyDeleteCheck out tat video...
Deleteduring execution is shows this error then what can i do for this error ?
DeleteMyServer.java:3: error: package javax.servlet does not exist
import javax.servlet.*;
If u set the environment variables correctly, you wont get this error msg.
Deletehi admin i ma getting this error HTTP Status 404 - /servlets3/
ReplyDeletetype Status report
message /servlets3/
description The requested resource is not available.
pleas help machi any way nice info
if you give the url correctly in your program, u wont get this error msg. it is case sensitive too..
Deletewhat is that servlets3 in "http://localhost:8080/servlets3/MyServer" ??
ReplyDeleteservlets is the folder created for this project in tomcat.
Deleteservlets3 is the folder created for this project in tomcat.
Deleteservlets is the folder created for this project in tomcat.
ReplyDeletewhere is output this program....
ReplyDeletehow to run this in netbeans
ReplyDeleteNOT WORKING
ReplyDeletePost your output and by the way why are you using web.xml? no use for that !!! #Lol #Ani
ReplyDeleteWhere is output?
ReplyDelete