Solved

Using URL connection in Applet ...

Posted on 1997-08-10
6
299 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
How  can  i  resolve  HTTP Status 404 -? 8 43
sql import cannot be resolved jsp 3 48
learn programming 8 42
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

837 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