Servlet with an embedded applet


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.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";

        PrintWriter out = response.getWriter();

        out.println("<body bgcolor=\"white\">");

        out.println("<title> From servlet to applet </title>");
        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.print("<form action=\"");
        out.print("\" ");
        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\">");


    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 "";

This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at

Spring Semester 2002