tomcat下jsp嵌入applet找不着类的解决方案

tomcat下jsp嵌入applet找不着类的解决方案


背景:eclipse + tomcat + webProject
问题:webProject首页中使用了applet嵌入,结果提示applet类无法查找
解决方案:在应用程序根目录下新建 applet 文件夹,将编译好的 applet.class 文件考到该文件夹下。

详细情况:
1、新建项目 MyApplet, index.jsp 源码:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>HelloWorld! Applet</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> This is my JSP page. <br> <applet code="com.defonds.applet.HelloApplet.class" width=200 height=100 ></applet> </body> </html>
2、新建 applet 类 HelloApplet 源码:
package com.defonds.applet; import java.applet.Applet; import java.awt.Graphics; public class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello,Defonds!", 5, 35); } }
3、将 MyApplet 部署到 tomcat 容器下。启动 tomcat,访问 MyApplet。页面提示错误信息如下:
载入:找不到类 com.defonds.applet.HelloApplet.class。 java.lang.ClassNotFoundException: com.defonds.applet.HelloApplet.class at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source) at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source) at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.io.IOException: open HTTP connection failed:http://localhost:9999/MyApplet/com/defonds/applet/HelloApplet/class.class at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source) at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source) at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) ... 7 more 异常:java.lang.ClassNotFoundException: com.defonds.applet.HelloApplet.class

分析:applet 的 class 文件最好不要放在 web-inf 目录下,因为这是个特殊目录,所以 http://localhost:9999/MyApplet/ 不能找到  com.defonds.applet.HelloApplet.class 这个文件。可以在 /webapps/MyApplet/ 下新建一个文件夹,将 applet 的 class 文件放在里面,在重新设置 codebase 属性。
这样,新的目录结构如下:
 测试用的 jsp     /webapps/MyApplet/index.jsp  
 jsp 中插入的applet class     /webapps/MyApplet/applet/com/defonds/applet/HelloApplet.class
URL
 
http://localhost:9999/MyApplet/index.jsp 
新的 index.jsp 源码如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>HelloWorld! Applet</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> This is my JSP page. <br> <applet code="com.defonds.applet.HelloApplet.class" width=200 height=100 codebase="/MyApplet/applet/"></applet> </body> </html>

redeploy 一下 tomcat,重新访问 MyApplet 项目,发现 applet 已经成功嵌入。

 

你可能感兴趣的:(html,tomcat,jsp,ClassLoader,applet,stylesheet)