We help IT Professionals succeed at work.

Check out this week's podcast, "Dairy Farms to Databases: Community's Hand in Technology"Listen Now

x

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

KE
KE asked
on
318 Views
Last Modified: 2012-05-05
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 :-)
Comment
Watch Question

Commented:
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.



KE

Author

Commented:
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 ?

Commented:
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.

KE

Author

Commented:
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 ?

KE

Author

Commented:
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.
KE

Author

Commented:
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...
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.