Solved

Using URL connection in Applet ...

Posted on 1997-08-10
6
293 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now