在html页面嵌入applet异常处理办法

来源:互联网 发布:ubuntu 聊天工具 编辑:程序博客网 时间:2024/06/02 13:12

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"> Ifyour browser was Java-enabled, a "Hello, World" message would appearhere. </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


原创粉丝点击