简体   繁体   English

在Web应用程序中运行applet

[英]run applet in web application

I want to run simple applet in my web application using html applet tag but it gives error like 我想在我的Web应用程序中使用html applet标签运行简单的applet,但它给出了错误

java.lang.ClassNotFoundException: MyApplet java.lang.ClassNotFoundException:MyApplet

please, give me sample application if possible ..... 请尽可能给我样品申请.....

the problem is that the applet engine can't find your MyApplet class at the codebase you have defined. 问题是applet引擎无法在你定义的代码库中找到你的MyApplet类。

This can be caused because you have you class at your /WEB-INF/classes directory. 这可能是因为您在/ WEB-INF / classes目录中有课程。 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. 该目录受servlet引擎保护,因为它不能从外部资源加入(可以是JSP / HTML页面上的applet标记)。

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 (ie the jsp folder of your web application). 最简单的方法是将您的MyApplet类打包为一个jar文件(让我们称之为myapplet.jar ),并将其保存在一个可访问的目录(即Web应用程序的jsp文件夹)中。 As an example, supose you have the following folders for the web application: 例如,假设您为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保存在applet文件夹中,并像这样设置applet标签配置(假设您的Web上下文是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 在这里您可以找到有关applet标签的更多信息: http//docs.oracle.com/javase/tutorial/deployment/applet/index.html

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). 旧线程,我知道......但是我想出了一个小小的hack,允许你提供WEB-INF / classes文件夹中的applet,这样你的项目中就不需要额外的jar了(而且你可以更快地重新部署您的applet)。 The downside of this is that you can't sign your applet (because it's a .class not a jar). 这样做的缺点是你不能签署你的applet(因为它是一个.class而不是jar)。 Let's cut to the chase here... 让我们切入这里追逐......

First, create a little servlet that serves applets (it requires Javassist): 首先,创建一个为applet提供服务的小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: 最后,从您的页面调用您的applet:

<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获取类的字节代码并将其提供给请求。

Disclaimer If 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提供实际为applet的类。

Check 2 things. 检查2件事。 1. the codebase is correct. 1.代码库是正确的。 To check that it is correctly written compose full URL (URL of your page + codebase) and try it directly in browser. 要检查是否正确编写了完整的URL(页面的URL +代码库)并直接在浏览器中进行尝试。 Be sure that it is correct. 确保它是正确的。

  1. 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 如果它不起作用,请在此处发布您的标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM