Solved

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

Posted on 1998-05-15
9
249 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 :-)
0
Comment
Question by:KE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Expert Comment

by:evijay
ID: 1221065
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.



0
 
LVL 3

Author Comment

by:KE
ID: 1221066
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 ?

0
 
LVL 4

Expert Comment

by:evijay
ID: 1221067
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.

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 3

Author Comment

by:KE
ID: 1221068
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 ?

0
 
LVL 3

Author Comment

by:KE
ID: 1221069
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).

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221070
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?
0
 
LVL 2

Expert Comment

by:webster030697
ID: 1221071
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.
0
 
LVL 3

Author Comment

by:KE
ID: 1221072
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...
0
 
LVL 4

Accepted Solution

by:
evijay earned 200 total points
ID: 1221073


To read file from local file system
go to this example


http://java.sun.com/security/signExample/index.html

This uses javakey. Now, for netscape you have to use zigbert and for ie you have to use cabsign tools

The procedure is available in

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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

635 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