Servlet with an embedded applet
File AppletInServlet.java
The servlet creates a web page with an embedded applet in it. The applet class
is downloaded from puma. The <EMBED> tag passes a parameter StudentName to
the applet. The web page created by the servlet has a form where user can enter
a search string. The button "Look it up" reloads the page and resets the parameter
for the applet. When the button "GO" on the applet is pressed, the applet
performs the search for the pattern that has been entered in the field "First
Name".
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AppletInServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// getting the form's parameter
String search = request.getParameter("First");
// by default the search string is "Mary"
if (search == null)
search = "Mary";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
out.println("<title> From servlet to applet </title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>This servlet creates an HTML document");
out.println(" which contains an applet</h2>");
// wrapping the EMBED tag around the applet
// and the parameter
out.println(embedApplet("McKoiAppletParam.class", search));
out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("AppletInServlet"));
out.print("\" ");
out.println("method=POST>");
out.println("Please type in a student's first name<BR>");
out.println("The entire name or just a part of a name<P>");
out.println("If no name is entered, the search is string is \"Mary\"<P>");
out.println("First name: ");
out.println("<input type=text size=15 name=\"First\"><P>");
out.println("<input type=submit value=\"Look it up\">");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
// return a string containing the EMBED tag for the applet
// the Java version is set to 1.4,
// the ARCHIVE tag has the reference to mckoidb.jar
// We assume that the mckoidb.jar file is in the same directory
// as the applet class
// The extra field StudentName is used to pass the parameter
// to the applet
public String embedApplet(String appletName, String param) {
String s = "<EMBED type=\"application/x-java-applet;version=1.4\"";
s = s + "CODE = \"" + appletName + "\"";
s = s + "CODEBASE = \"" + prefixPuma() + "\"";
s = s + "ARCHIVE = \"" + "mckoidb.jar\"";
s = s + "WIDTH = 800 HEIGHT = 300 ";
s = s + "StudentName = \"" + param + "\">";
s = s + "</EMBED>";
return s;
}
// returns the HTTP adress of puma as a string
public String prefixPuma() {
return "http://cs.wellesley.edu/~ecom/download/";
}
}
This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at emachkas@wellesley.edu
Spring Semester 2002