1、java代码
public class HelloWorldApplet extends Applet {
/**
* @Description: TODO(这里用一句话描述这个类的作用)
* @date 2017年7月4日 上午8:49:56
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
g.drawString("Hello World", 25, 50);
}
}
引入的架包:
import java.applet.*;
import java.awt.*;
2、编写html页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String lang = "" + session.getAttribute("lang");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hr>
<applet code="pers.wxp.applet.HelloWorldApplet.class" codeBase="."
archive="<%=basePath%>/jar/helloword.jar" width="320" height="120"> If
your browser was Java-enabled, a "Hello, World" message would appear
here. </applet>
<hr>
</body>
</html>
(1)一定加入以下代码,获取工程根目录
%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String lang = "" + session.getAttribute("lang");
%>
不然一直报找到的类为空异常。
(2)code="pers.wxp.applet.HelloWorldApplet.class"写的是包名+类名。单独写类名同样报错。
(3)必须将此类打包成jar文件,如何打包如下:
详细介绍:http://jingyan.baidu.com/article/5bbb5a1b280d0113eba179ce.htm
archive="<%=basePath%>/jar/helloword.jar"
4、html标签的<applet>介绍
详细介绍:http://www.runoob.com/tags/tag-applet.html