1

我在netbeans中有两个项目。一个与我的 web 和我的 servlet 一起使用。第二个项目名为 Applet,包含我的小程序(自签名小程序)和他需要工作的类。小程序通过打包选项添加到 Web 项目中,并且在浏览器中运行良好。我有一个方法可以打开 URLconnection 到 servlet 并发送 GET 请求,servlet 发送响应,然后我将 aplet 中的 inputStream 获取到 jLabel 文本。在浏览器中一切正常,但是当我在 Appletviewer 中尝试它时,它会触发异常,并且在 HTTP 服务器监视器中没有出现对服务器的新 GET 请求,这应该在 UrlConnection.getInputstream() 完成时发生。那是我的问题吗?那么是否存在从 Appletviewer 连接到 servlet 的任何解决方案?

这是我从servlet获取文本的方法

 URL url = null;
        try {
            url = new URL("http://localhost:8084/Rozhlas_jsf/SoundServlet?name=startSounds");
        } catch (MalformedURLException ex) {
            Logger.getLogger(SoundApplet.class.getName()).log(Level.SEVERE, null, ex);
        }

// open a connection to the servlet
URLConnection servletConnection=null;
        try {
            servletConnection = url.openConnection();
            servletConnection.setDoInput(true);
            servletConnection.setDoOutput(true);
        } catch (IOException ex) {
            Logger.getLogger(SoundApplet.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            // get the input stream from the servlet
            InputStream in = servletConnection.getInputStream();
           jLabel1.setText(convertStreamToString(in)) ;


        } catch (IOException ex) {
            Logger.getLogger(SoundApplet.class.getName()).log(Level.SEVERE, null, ex);
        }

这是我在小程序查看器中尝试时发生的情况

 compile-single:
run-applet:
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:8084" "connect,resolve")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
    at java.security.AccessController.checkPermission(AccessController.java:555)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:466)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:213)
    at sun.net.www.http.HttpClient.New(HttpClient.java:300)
    at sun.net.www.http.HttpClient.New(HttpClient.java:316)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:992)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:928)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1296)
    at SoundApplet.SoundApplet.jButton5ActionPerformed(SoundApplet.java:683)
    at SoundApplet.SoundApplet.access$200(SoundApplet.java:44)
    at SoundApplet.SoundApplet$3.actionPerformed(SoundApplet.java:184)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

问题解决了!!!

我今天早上尝试更改 applet.policy 文件,因为我试图解决这个问题 我的另一个问题

我覆盖了这个

grant {

permission java.security.AllPermission;
};

这样

 grant {

    permission java.io.FilePermission "<<ALL FILES>>","write";
    };

通过它,我失去了从 appletviewer 连接我的 Servlet 的权限。我只是将其重写回来,一切都很好。对不起,不必要的话题,但我真的很生气。

4

0 回答 0