java 在 Web 应用程序中运行小程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4253786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
run applet in web application
提问by chetan
I want to run simple applet in my web application using html applet tag but it gives error like
我想使用 html applet 标签在我的 web 应用程序中运行简单的小程序,但它给出了类似的错误
java.lang.ClassNotFoundException: MyApplet
java.lang.ClassNotFoundException: MyApplet
please, give me sample application if possible .....
如果可能,请给我示例应用程序.....
回答by Tomas Narros
the problem is that the applet engine can't find your MyApplet class at the codebase you have defined.
问题是小程序引擎在您定义的代码库中找不到您的 MyApplet 类。
This can be caused because you have you class at your /WEB-INF/classes directory. This directory is protected by the servlet engine, for it not to be accesed from external resources (as can be an applet tag at a JSP/HTML page.
这可能是因为您在 /WEB-INF/classes 目录中设置了类。该目录受 servlet 引擎保护,因为它不能从外部资源访问(可以是 JSP/HTML 页面上的小程序标记。
There are a few ways to solve this. The easiest one is to pack your MyApplet class un a jar file (let's call it myapplet.jar
), and save it at an accesible directory (i.e. the jsp folder of your web application).
As an example, supose you have the following folders for the web application:
有几种方法可以解决这个问题。最简单的方法是将您的 MyApplet 类打包成一个 jar 文件(我们称之为myapplet.jar
),并将其保存在一个可访问的目录中(即您的 Web 应用程序的 jsp 文件夹)。例如,假设您有以下 Web 应用程序文件夹:
/MyWebApp/jsp
/MyWebApp/applet
/MyWebApp/WEB-INF
The client browsers can access the content of jsp and applet folders.
客户端浏览器可以访问jsp 和applet 文件夹的内容。
Then, save your myapplet.jar at the applet folder, and set your applet tag configuration like this (suposing that you web context is MyWebApp):
然后,将 myapplet.jar 保存在小程序文件夹中,并像这样设置小程序标记配置(假设您的网络上下文是 MyWebApp):
<applet codebase="/MyWebApp/applet" archive="myapplet.jar"
code="MyApplet.class" width="600" height="500">
</applet>
Here you can find more info about the applet tag: http://docs.oracle.com/javase/tutorial/deployment/applet/index.html
在这里您可以找到有关小程序标记的更多信息:http: //docs.oracle.com/javase/tutorial/deployment/applet/index.html
回答by Andre
Old thread, I know... but I've come up with a little hack that allows you to serve applets that are inside your WEB-INF/classes folder so that you don't need an extra jar in your project (and you can redeploy your applet a little faster). The downside of this is that you can't sign your applet (because it's a .class not a jar). Let's cut to the chase here...
旧线程,我知道...但我想出了一个小技巧,它允许您提供 WEB-INF/classes 文件夹中的小程序,这样您的项目中就不需要额外的 jar(并且您可以更快地重新部署您的小程序)。这样做的缺点是你不能签署你的小程序(因为它是一个 .class 而不是 jar)。让我们切入正题……
First, create a little servlet that serves applets (it requires Javassist):
首先,创建一个服务小程序的小 servlet(它需要 Javassist):
public class AppletServlet implements Servlet {
...
ClassPool pool = ClassPool.getDefault();
@Override
public void init(ServletConfig config) throws ServletException {
pool.insertClassPath(new ClassClassPath(this.getClass()));
}
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
String className = ((HttpServletRequest) req).getPathInfo().substring(1);
try {
CtClass cc = pool.get(className.replace("/", ".").replace(".class", ""));
res.setContentType("application/x-java-applet;version=1.5.0");
res.setContentLength(cc.toBytecode().length);
res.getOutputStream().write(cc.toBytecode());
res.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
...
}
Now declare your AppletServlet (I know, terrible name) as a servlet in your web.xml:
现在将您的 AppletServlet(我知道,可怕的名字)声明为您的 web.xml 中的 servlet:
<servlet>
<servlet-name>Applet Servlet</servlet-name>
<servlet-class>com.example.AppletServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Applet Servlet</servlet-name>
<url-pattern>/applet/*</url-pattern>
</servlet-mapping>
Finally, invoke your applet from your page:
最后,从您的页面调用您的小程序:
<object type="application/x-java-applet" height="300" width="550">
<param name="codebase" value="applet/" />
<param name="code" value="com.example.MyApplet" />
<param name="teste" value="teste"></param>
Applet failed to run. No Java plug-in was found.
</object>
And that's it. The servlet will use Javassist to get the byte code for your class and serve it to the request.
就是这样。servlet 将使用 Javassist 获取您的类的字节码并将其提供给请求。
DisclaimerIf someone knows your package structure, they could download all the classes and do evil things from there. So make sure you only allow the servlet to serve classes that are actually applets.
免责声明如果有人知道你的包结构,他们可以下载所有的类并从那里做坏事。因此,请确保您只允许 servlet 为实际上是小程序的类提供服务。
回答by AlexR
Check 2 things. 1. the codebase is correct. To check that it is correctly written compose full URL (URL of your page + codebase) and try it directly in browser. Be sure that it is correct.
检查 2 件事。1. 代码库正确。要检查它是否正确编写,请编写完整的 URL(页面的 URL + 代码库)并直接在浏览器中尝试。确保它是正确的。
- The class name is written correctly. It must be fully qualified class name (including package name)
- 类名写得正确。它必须是完全限定的类名(包括包名)
If it does not work, post your tag here
如果它不起作用,请在此处发布您的标签