• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Writing a file from an Applet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a small Applet and I need to be able to write to a file from where the Applet came from. I am using a getDocumentBase() to get access to this path. So far I keep getting security exceptions when I try to write. Please give me a code fragment that allows me to write to a Text file from an Applet.
Thanks
Bo
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please see this related discussion.
Also, your name is NOT in accordance with javaranch policy.
Pl. visit http://www.javaranch.com/name.jsp and re-register.
Thanks for your cooperation.
regds.
- satya
 
koralage
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How exactly do I change my user name. Also I have looked at the links you gave me and so far I don't have a clear picture. I will do some more looking into, but I might have to ask for help again.
Thanks
Bo
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bo,
Thank you for adhering to the policy. I appreciate your input in this forum very much. I am glad that you understand that we are just trying to maintain a professional atmosphere.
You might have missed the section in our naming policy page that said:


If you are currently using an account that uses an inappropriate alias, please create a new account that follows the rules. The software we use here is not capable of changing the name on an existing account and we have unlimited account slots.
I want to maintain a professional exchange on JavaRanch so that corporate america won't feel embarassed to find employees spending time here.


Unfortunately, as it says, it is not possible to change the user name. To continue posting here, you must re-register with a new name. Here is the link where you can do that: http://www.javaranch.com/cgi-bin/ubb/Ultimate.cgi?action=agree
I hope that you will complete this process so that we can get on to answering your questions and getting your valuable input on the questions of other ranchers.
Thanks again.
Stephanie
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well about signing or giving permission to applets I have had no luck so far. I went through resources you gave and tried to follow few of the examples from sun. I am not clear about setting properties and signatures. Is there any simple example I can look at. All I need to do is to read and write to the server where the applet came from. Thanks for your help
Bo
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bo!
It is a little difficult to trouble-shoot without seeing your code. How are you attempting to write this file on the server? One option would be to use sockets. There is a very good and simple example of how to implement sockets here: http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
The section titled "Writing a Client/Server Pair" might work for you here.
The section titled "Using a Server to Work Around Security Restrictions", here: http://java.sun.com/docs/books/tutorial/applet/practical/workaround.html
might also help.
If this is not what you had in mind or if I have misunderstood your goal, please post again so that we can find a better solution.
Thanks.
Stephanie
 
Bo Koralage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I finally I got the Applet to read and write a file. What I had to do was to write a new security policy and add that new policy to my java.security file in the JRE folder. I also required users to use the java plugin. Now I am in the process of moving my files to a web server. The problem I am running into is reading the file from withing the server. I am still using the original code I was used to read the file.
String fileName = "data/test1.dat";
URL url = new URL(myApplet.getDocumentBase(), fileName);
File datFile = new File(url.getFile());
System.out.println(datFile);
if (datFile.exists()){
BufferedReader in = new BufferedReader(new FileReader(datFile));
String line = in.readLine();
....
}

I put in the println for debugging. On the java console it does show me a valid directory file path. However, I am not getting any data for my app and I don't get any security violations.
I guess the bottom line is can an applet open a file from a root webserver directory just as opening one on the file system. Any help you can give is greatly appreciated.
Thanks
Bo
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a wild shot:-
Have you tried
URL url=new URL(...);
URLConnection con=url.openConnection();
con.connect();
InputStream is=new con.getInputStream();
BufferedReader in= new BufferedReader(new InputStreamReader(is));
....

 
Bo Koralage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that works for reading a file. But I am still not able to write to a file. Here is my last try.
String fileName = "data/temp.dat";
URL url = new URL(calApplet.getDocumentBase(), fileName);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
out = new PrintWriter(connection.getOutputStream());
out.write("This is a test",0, outString.length());
out.write('\n');
Before this when I had the Applet as a file in the dirctory I was using FileWriter and I didn't have any problems. The FileWriter doesn't seem to work when going from a webserver. I would appreciate any help.
Thanks
Bo
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the above method will retrieve the contents of the resource (file), not modify it.
To write to the file, you'll need to have a program running on the server which will receive the data from the client, and then write to the file using FileWriter etc.
One way is to have a simple java server program (say FileWriterServer), running on some port (say 5000), on the same host as your webserver. This program would listen by opening a ServerSocket. The applet will then open a socket connection with the FileWriterServer. Then through the socket, the applet will send the data. The FileWriterServer, will receive the data through the socket, and using the FileWriter, write to the file.
The FileWriterServer could also be implemented as RMI Server exposing a remote method writeFile(String, fileURL). The RMI Server would implement the writeFile method, using a FileWriter to write the String parameter to the File pointed by the URL. The applet would then need to call writeFile() method, whenever it needs to write to the file.
Another way to do it is through a servlet. Make a servlet, implementing its doPost method, to receive Post data and then using FileWriter, write that data to the file. This servlet can be called through the URLConnection.
Waiting to hear your comments.
 
Bo Koralage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of these suggestions sound good, but they all require other java apps or servers to be wrtten. I would think that applets should be able to write a basic text files. Is this a limitation of Applets?
Basically I don't know why I have to implement a whole other server app to just write a basic text file. Seems like Sun shoud provide some functionality for this. Please tell me your thoughts
Thanks
Bo
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want the applet to write to the local host? Or do you want the applet to write to a file on a remote host from where it was downloaded?
 
Bo Koralage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To the remote host where the applet came from. I already wrote a security policy on the local host to give that permission. I am using the Java plugin so all users will use the JRE which was the best way I saw to keep all users consitant. Thanks for taking the time to help
Bo
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you can't directly write a file on the remote host whether you are using an applet, or an application. There must be a server/servlet running on the remote host, to write the file on the remote host. And this is true whether you are using Java or C or C++ ...
A program running on one host cannot directly access the file-system/local resources of another host. Remember that even for reading a file from the remote host you are using a server (eg. web server). You can request the web-server to serve up the contents of a (static) resource or to initiate a program (CGI/Servlet) and then serve up the response of that program but a web-server is not designed to directly write to a file. So use the web-server to call a servlet to write to the file. Or have a server (on a different port from the web-server), to write to the file. Programming a server/servlet is almost a trivial task in Java. It can be done in a few lines of code.
 
Bo Koralage
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response. I will give that a try.
Bo
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic