Solved

Using URL connection in Applet ...

Posted on 1997-08-10
6
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

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

Author Comment

by:feng042497
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
java continue statement 10 71
IT Company 5 68
starter POM and spring-boot-starter,  spring-boot-web 2 10
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)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

772 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

13 Experts available now in Live!

Get 1:1 Help Now