Solved

Using URL connection in Applet ...

Posted on 1997-08-10
6
296 Views
Last Modified: 2006-11-17
I want to link a URL connection in my applet but it seems like applet security package doesn't allow me to do that.  But I do see someone write some applets which can telnet or connect to remote sites.  So I believe there must be some tricks in that.  Do you know how to circumvent this obstacle?  Thanks.

Gang
0
Comment
Question by:feng042497
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
markoj earned 50 total points
ID: 1224702
Your applet should communicate with a application (you could use servlet) running on your server, which opens connections and retrieves data.

0
 

Author Comment

by:feng042497
ID: 1224703
Hi,
      Thank you for your answer.  But I wonder if every time I want to link to a URL, I have to write a servlet.  Actually, my code is copied from a book.  And even more, I just want to communicate with the server on which the applet itself resides.  According to what I read from a lot of book, this should work under such environment.  The error message is as following.

*** Security Exception: Couldn't connect to 128.194.135.90 with origin from  ***
sun.applet.AppletSecurityException: security.Couldn't connect to 128.194.135.90 with origin from
        at sun.applet.AppletSecurity.checkConnect(AppletSecurity.java:419)
        at sun.applet.AppletSecurity.checkConnect(AppletSecurity.java:464)
        at sun.applet.AppletSecurity.checkConnect(AppletSecurity.java:366)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:103)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:440)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:75)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:166)
        at java.net.URL.openStream(URL.java:348)
        at cgitest.updateText(cgitest.java:38)
        at cgitest.init(cgitest.java:16)
        at sun.applet.AppletPanel.run(AppletPanel.java:259)
        at java.lang.Thread.run(Thread.java)
sun.applet.AppletSecurityException: security.Couldn't connect to 128.194.135.90 with origin from
        at sun.applet.AppletSecurity.checkConnect(AppletSecurity.java:419)
        at sun.applet.AppletSecurity.checkConnect(AppletSecurity.java:464)
        at sun.applet.AppletSecurity.checkConnect(AppletSecurity.java:366)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:103)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:440)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:75)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:166)
        at java.net.URL.openStream(URL.java:348)
        at cgitest.updateText(cgitest.java:38)
        at cgitest.init(cgitest.java:16)
        at sun.applet.AppletPanel.run(AppletPanel.java:259)
        at java.lang.Thread.run(Thread.java)

Do you have any idea about that?  Thanks.

Gang
0
 

Expert Comment

by:markoj
ID: 1224704
I'm not sure I understand what you mean.
If writing a new servlet means coding, then the answer is no.
You just have to run another instance. That server-application
opens the desired connections and delivers the results to your
applet running on the client.
As for the second part: is the computer applet on which applet
resides the host or the client. If 128.194.135.90 is the client
then it's normal you get the security exception. If 128.194.135.90 is your server, there should be no problem opening
the connection. Can you show some code?

0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 1

Expert Comment

by:sunot
ID: 1224705
If it's just for data entry and retrieval, why would you need applets since you already have servlets?
0
 

Author Comment

by:feng042497
ID: 1224706
OK.  Here is my simple applet code:

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class cgitest extends Applet
{
        URL url;
        TextArea text=new TextArea(24, 80);
        Button button=new Button("Again");
        DataInputStream inStream;

        public void init()
        {
                setLayout(new BorderLayout());
                updateText();
                add("Center", text);
                add("South", new Button("Again"));
        }

        public boolean hadleEvent(Event event)
        {
                if (event.target instanceof Button)
        {
                if (event.target instanceof Button)
                {
                        if ("Again".equals(event.arg))
                        {
                                updateText();
                                return true;
                        }
                }
                return false;
        }

        void updateText()
        {
                try {
                        url=new URL("http://dasher/");
                        inStream=new DataInputStream(url.openStream());
                        String newText="";
                        String line;
                        while((line=inStream.readLine())!=null)
                                newText+=line+"\n";

                        text.setText(newText);

                        text.setText(newText);
                } catch (MalformedURLException ex) {
                        text.setText("MalformedURLException");
                } catch (IOException ex) {
                        text.setText("IOException");
                }
        }
}

The site http://dasher/ is just where this applet resides.
0
 

Author Comment

by:feng042497
ID: 1224707
I'm sorry to bother you.  I found I made a mistake.  Actually, it should work.  I got those error message because I test my program under appletviewer environment.  When I test these program directly under Netscape, they work.  So, you see, there are a lot of things incompatible between appletviewer and browser when running applets.  In fact, I found some other incompatibilities before.  I think Sun should make improvement of the appletviewer utility to let it like under true browser environment.
      Thank you for your efforts and time.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question