Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2003

Tip: Looking for answers? Try searching our database.

Applet reading from http server ?

Thread view: 
Francisco Camarero - 11 Jul 2003 12:56 GMT
I am writing a very small Applet that reads some files via
http from different servers and reports some result.

I am using the usual code:

..
MyURL = new URL("http://www.somewhere.com/somepath/somefile");
InFile = new BufferedReader(new InputStreamReader(MyURL.openStream()));
inputLine = InFile.readLine();
..

But, when executing it with appletviewer I get exceptions:

java.security.AccessControlException: access denied (java.net.SocketPermission
www.somewhere.com resolve)

And from the Browser it doesn't work either.

I have read somewhere that this is normal... that because of
security, the applet can't read via http from other servers...

I have also read something about a workaround with servlets installed
on the servers I want to read from, but I don't control these servers,
so I can't install nothing there...

Any other solution or definitive negative ?

Thanks!

Fran.
Ike - 11 Jul 2003 14:32 GMT
Use a test certificate -- go to java.sun.com and read how to create a test
certificate and use it to digitially sign. The other alternative, for you to
test locally, is to run it as an application. To do this, put a main() in
the applet, which instantiates the applet, as follows:

public class MyApplet extends Applet{

public boolean isAnApplet;

public class MyApplet(){
   isAnApplet=true;
}

public static void main(String []args) {
       MyApplet applet = new MyApplet ();
       JFrame mainFrame = new JFrame(applet.getClass().getName());
       applet.isAnApplet=false; //VITAL! this is a MyApplet class member
variable which you can use throughtout your code to tell you if you are
running in applet or application mode.
       applet.init();
       applet.start();
       mainFrame.getContentPane().add(applet);
       mainFrame.setSize(applet.windowsize); //width, height
       mainFrame.setVisible(true);
       mainFrame.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });
   }
}

//now you can run it as an applet or an applicatrion. If you run as an
applet, to circumvent the security restrictions, digitally sign it - even
with a test certificate. -Ike

> I am writing a very small Applet that reads some files via
> http from different servers and reports some result.
[quoted text clipped - 26 lines]
>
> Fran.
Francisco Camarero - 11 Jul 2003 16:47 GMT
I have tried the certificate solution:

  javac HelloWorld.java
  jar cvf HelloWorld.jar HelloWorld.class
  jarsigner -keystore myKeystore HelloWorld.jar myself

and placed the jar and the html file in my web server.

See codes:

index.html:

<html>
<head>
</head>
<body>
<applet archive="HelloWorld.jar" code="HelloWorld.class" width=150
height=50></applet>
<br>
<br>
In the applet field above it should appear the text :  
<br>
<br>
&lt;html&gt;&lt;head&gt;
<br>
<br>
<br>
instead, you can see:
<br>
<br>
Empty Text
<br>
<br>
</body>
</html>

HelloWorld.java:

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

public class HelloWorld extends Applet {

 
 //This causes the Browser to Redirect, and is not what we want...
 //public void init() {
 //  
 //AppletContext appletContext;
 //URL my_url;
 //  try {
 //    my_url = new URL("http://www.yahoo.com");
 //    appletContext = getAppletContext();
 //    appletContext.showDocument(my_url);
 //  } catch (Exception e) {
 //    System.out.println("Exception!");
 //  }
 //}
 
 URL MyURL;         
 BufferedReader InFile;  
 String inputLine;     

 public void init() {
   inputLine = "Empty Text";
   try {
     MyURL = new URL("http://www.yahoo.com");
     InFile = new BufferedReader(new InputStreamReader(MyURL.openStream()));
     inputLine = InFile.readLine();
   } catch (Exception MyException) {
     System.out.println(MyException);
   }
 }

 public void paint(Graphics g) {
   g.drawString(inputLine, 50,25);
 }
}

When I browse that index.html file, I get the "Empty Text" string, and
not the first line of the www.yahoo.com server...

Am I doing something wrong?...

> Use a test certificate -- go to java.sun.com and read how to create a test
> certificate and use it to digitially sign. The other alternative, for you to
[quoted text clipped - 63 lines]
> >
> > Fran.

Signature

Francisco Camarero                                          GSM: +41 764 5432 78
ETZ J86, Gloriastrasse 35                             telephone: +41 1 632 76 50
CH-8092 Zurich                                        telefax:   +41 1 632 11 94
Switzerland                                      e-mail: camarero@iis.ee.ethz.ch

Roedy Green - 12 Jul 2003 04:30 GMT
>   MyURL = new URL("http://www.yahoo.com");

try running as an application first.  then once it is working, flip to
applet and sign.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Ike - 12 Jul 2003 04:34 GMT
I dont think you can have an applet read from a server OTHER than the server
that it resides on. Unless your applet resides on http://www.yahoo.com I
dont think you can read from that -Ike

> I have tried the certificate solution:
>
[quoted text clipped - 148 lines]
> > >
> > > Fran.
Francisco Camarero - 14 Jul 2003 10:40 GMT
So, I think that looks like a Definitive Negative answer to my problem...

Thanks anyway!

> I dont think you can have an applet read from a server OTHER than the server
> that it resides on. Unless your applet resides on http://www.yahoo.com I
> dont think you can read from that -Ike
Roedy Green - 12 Jul 2003 04:27 GMT
> MyURL = new URL("http://www.somewhere.com/somepath/somefile");

Unsigned applets are only permitted to read from the machine they were
loaded from.  

See http://mindprod.com/jgloss/applet.html
http://mindprod.com/jgloss/signedapplet.html
http://mindprod.com/jgloss/javawebstart.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.