Servlet程序:ServletPage1.java
package ServletAndAppletComunication;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ServletPage1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>ServletPage1</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<center><b>Servlet通过param标记传递参数给Applet</b>");
out.println("<br><applet code=\"AppletPage1\""+ "width=\"200\" height=\"200\">");
out.println("<param name=\"Data\" value=\"");
out.println(java.text.DateFormat.getDateInstance().format(new java.util.Date())+"\">");
out.println("</applet>");
// out.println("<p>The servlet has received a " + request.getMethod() + ". This is the reply.</p>");
out.println("</center>");
out.println("</body></html>");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
Applet程序: AppletPage1.java
package ServletAndAppletComunication;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletPage1 extends Applet
{
Label label = new Label();
//Construct the applet
public AppletPage1()
{
}
//Initialize the applet
public void init()
{
try {
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
this.setSize(200,200);
label.setText("服务器时间是:"+this.getParameter("Data"));
this.add(label,null);
}
//Get Applet information
public String getAppletInfo()
{
return "读取Servlet传过来的参数";
}
}