通过netbeans中的appletviewer与另一个项目中的servlet进行通信

时间:2012-11-25 23:26:23

标签: java servlets netbeans web applet

我在netbeans中有两个项目。一个是我的网络和我的servlet。第二个项目名为Applet,包含我需要工作的applet(自签名applet)。 applet通过打包选项添加到Web项目中,在浏览器中运行良好。我有一个方法打开URL连接到servlet并发送GET请求,servlet发送响应,我在我的aplet中获取inputStream到jLabel文本。所有工作都在浏览器中工作,但是当我在Appletviewer中尝试它时,它会触发异常,并且在HTTP服务器中监视器不会向UrlConnection.getInputstream()完成时出现对服务器的新GET请求。这就是我的问题?那么存在从Appletviewer连接到servlet的任何解决方案吗?

这是我从servlet获取文本的方法,该文本在浏览器中使用我的Web运行,但如果我通过appletviewer运行它则不行

 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);
        }

以下是我在applet viewer中尝试时发生的事情

 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文件,因为我试图解决这个问题 my another problem

我覆盖了这个

grant {

permission java.security.AllPermission;
};

由此

 grant {

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

通过它我失去了从appletviewer连接我的Servlet的权限。我只是重写了一下,一切都很好。对不必要的主题感到抱歉,但我真的很生气。

0 个答案:

没有答案