?
Solved

Using URL connection in Applet ...

Posted on 1997-08-10
6
Medium Priority
?
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
markoj earned 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

752 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