Problem writing to a file using a URLConnection...
Hi all...
I've been reading throught the sun's toturial on networking that you can find here:
http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html
this toturial provides a java program that uses a servelet to write some text. First, I don't know what a servelet is. Second, I can't understand to what is this program writing. That is, where does it store it's output.
what I am trying to do is to allow my applet to write some data to a txt file that resides in the same directory as the applet. So, how can i do that, or in other words, what is the simplest way to do that? Do I need to write a servelet or just open a connection to the txt file and start writing ?
regards,
[740 byte] By [
Raieda] at [2007-11-27 10:51:51]

> Hi all...
>
> I've been reading throught the sun's toturial on
> networking that you can find here:
>
> http://java.sun.com/docs/books/tutorial/networking/url
> s/readingWriting.html
>
> this toturial provides a java program that uses a
> servelet to write some text. First, I don't know what
> a servelet is. Second, I can't understand to what is
> this program writing. That is, where does it store
> it's output.
>
Servlets are part of J2EE, which is used to write middleware for servers. There are a number of tutorials on Servlets here and on the net. The output is simply going to the console (if running as an application) or likely to a server log (if running as a Servlet). The first example is a simple J2SE standard application. The second simply sends a string to a Servlet (called ReverseServlet) that reverses the text. (So 'text' becomes 'txet').
> what I am trying to do is to allow my applet to write
> some data to a txt file that resides in the same
> directory as the applet. So, how can i do that, or in
> other words, what is the simplest way to do that? Do
> I need to write a servelet or just open a connection
> to the txt file and start writing ?
>
If you want to write to a file, you do not need a Servlet (though you can of course do this as well from a Servlet). You should use java.io.FileOutputStream (and probably wrap that in a java.io.PrintWriter).
However, you will run into another problem. The applet security model prevents you from writing locally to the filesystem unless it is signed. To sign your applet, you need to do the following steps:
http://java.sun.com/developer/technicalArticles/Security/Signed/
> regards,
- Saish
Saisha at 2007-7-29 11:33:56 >

Also see the material in this search list. There is information on using signed applets, security policy files on the local computer, and writing files to the applet's server.
http://www.google.com/search?q=java+applet+write+file
> what I am trying to do is to allow my applet to write
> some data to a txt file that resides in the same
> directory as the applet. So, how can i do that, or in
> other words, what is the simplest way to do that? Do
> I need to write a servelet or just open a connection
> to the txt file and start writing ?
You need some server side code on the same server from which the applet is dispatched to read and save the text file from your applet, which must be a signed one. The server side code would typically be a servlet to which your applet would make an URLConnection.
This forum thread might help: http://forum.java.sun.com/thread.jspa?threadID=5195127
hiwaa at 2007-7-29 11:33:56 >

thanks for your responses...
this link: http://java.sun.com/developer/technicalArticles/Security/Signed/ says that:
"By default, applets have no access to system resources outside the directory from which they were launched"
and
"For a JDK 1.2 applet to access local system resources outside the directory from which the applet is launched, the applet must be granted explicit access to those resources"
which means (to me) that the applet can write to local system resources inside the directory from which the applet is launched without it being signed which is what I am trying to do. So, I think that there is a simpler way to allow an applet to write files locally inside the directory from which the applet is launched than signing it. So, does any body know what to do?
Raieda at 2007-7-29 11:33:56 >

On the client side on which the applet runs:
Local file access can be done by a signed applet.
On the server side from which the applet was dispatched:
Applet can open a Socket or URLConnection on its 'codebase' == his mother server.
Don't confuse the two.
hiwaa at 2007-7-29 11:33:56 >
