Link to home
Start Free TrialLog in
Avatar of KE
KE

asked on

How to read a file fram an Applet in NS and IE

Could anyone be so kind to explain, how I read a textfile in to an applet, in both NetScape Communicator, and Internet Explorer >4.0. I need a common way, so I can use the same applet. Please explain every security aspect involved.
Your solution should be able to read the file on either a local file system, or the net.

Need more information, just ask !!!

Sources is welcome :-)
Avatar of evijay
evijay

I want to know what you mean by a local file system. Is it the filesystem of the client on which the browser is running or is it the filesystem of the host from which the applet is loaded.

Just remember one thing, An applet is not allowed to open, read/write files, open a socket connection to any host other than the host from which it is loaded.

if it is the filesystem of the host from which the applet is loaded, you need to write a proxy server (a java application which listens at a particular socket port and receives and processes requests - use ServerSocket) at the host which need to be running when the applet gets executed. The applet uses a socket connection to connect to that proxy server and forwards the request of reading the file to the proxy server  which reads the file and sends it back to the applet thru the socket connection.

Now if you want an applet to read the file from web, again the applet passes the url to the proxy server application which inturn uses the URL class to access that url, read its content and pass it onto the applet thru the socket connection.

See the java telnet applet in developer.javasoft.com site which employes a proxy server to emulate the telnet application.



So, if you want to read file from the file system from which the applet has originated or from the net, no special security considerations are required. Just use a proxy server. If you feel that this approach is too cumbersome, you need to sign the applet. Again here comes the problems with the incompatabilities of browsers and there is no  way to sign the applet that supports both ie and netscape browsers (because of browser wars).

If you want to read the data from the local file system of the client browser system, you need to sign the applet (the procedure varies for different browsers).

For more info go to
http://www.developer.com/reference/library/1575212986/htm/toc.htm 
and see the java security chapter.

For more info on signing the applets for netscape and ie go thru these to articles which explain in step by step manner how to do that in both these browsers

http://www.javareport.com/features/9801/sommers.html
http://www.javareport.com/features/9802/sommers.html

Dont think that i am giving the pointers. The author of the articles is an expert and he can explain the things in a more clear way than me.



Avatar of KE

ASKER

Thanks, for your huge answer.

I know it's a little late, however I was about to change the text of the question with some additional information.

I need to read (and only read) 5 ASCII based data files and some images, into a memory structure in the applet. In the meantime between your answer, I was looking at various methods to do this.
I found that I might be able to put the files into the distributed JAR file. The documentation states, that there should be no security restrictions to a JAR file - and if so, I would be very happy.
What I'm trying to figure now, is how to read a file from an JAR archive.
Currently I'm using this method to read a file:
BufferedReader in = new BufferedReader(new InputStreamReader(theURL.openStream()));
(which fails in Netscape)
How do I get this to work with JAR archives ?

KE,

Whether it is a jar file or any other file, the basic rule is
"An applet cannot read or write files since the browser security manager doesnt allow it to do that".

You can go thru the applet security restrictions page if you want.
A jar file is a file like any other file and u will read it using jar api (very simple api). But still, note that since you want to read a file, you will have to open a file which the applet security manager doesnt allow !!.

I have the source code
for applet security manager (for applet viewer) and i saw that clearly the security manager is not allowing to do that.

There is no way that you can read the files from jar archive. If you found the answer, i would be glad to use that one. I went thru many books and finally i have to resort to understanding the source code for appletviewer security manager and i found that it is not allowed unless it is signed.

Avatar of KE

ASKER

OK, I understand.

But if you can load an image from a JAR into your toolbars etc. (without any security restrictions) then I suppose you can load an ASCII file as well - Well, I don't know if you actually can do it, but it sounds resonable.

However I'm seeking the easiest way of getting external data into my applet. I don't actually mind if it's compiled into the code, but I don't know how to do this either.

So what do you think is the easiest way of reading (or compiling) images and ASCII files into the memory of the Applet ?

Avatar of KE

ASKER

I've found that I can use getResourceAsStream(), and I guess the next part would be, to get the JAR signed for use in NetScape (IE runs perfect).

Actualy, you can read any file you want as a stream if you read it from the server from both MSIE and Netscape. The problems start once it has been written on the local file system.

Additionaly, there is a free class lib available that can read data and immage files from a JAR or zip file, you interested?
If you have access to the web server that is serving the applet, you could write a servlet to serve the files you need, and then the applet can get them through a socket connection to the servlet.
Avatar of KE

ASKER

jpk:
I'm looking for a solution, which will work on a local filesystem (i.e. CD-ROM). IE works perfectly, but NetScape throws a lot of exceptions, about verification etc.

webster:
OK, but as mentioned to jpk, I'm looking for a solution that will work on a local filesystem. As I understand, servlets is not supported unless you have a server ?

Thanks...
ASKER CERTIFIED SOLUTION
Avatar of evijay
evijay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial