- Everything You Need To Know about TCP/IP but Failed to Learn in Kindergarten
- A Client Socket in Java
- Sending Email by Java
- A Server Socket in Java
- HTTP and Web Browsing: Retrieving HTTP Pages
- How to Make an Applet Write a File on the Server
- A Multithreaded HTTP Server
- A Mapped I/O HTTP Server
- Further Reading
- Some Light Relief—Using Java to Stuff an Online Poll
How to Make an Applet Write a File on the Server
Everyone who writes an applet sooner or later wants to have it write a file back on the server. This is perhaps the most frequently asked question in Java. You might want the high score in a high score file, or have a guestbook that visitors to the page can sign, or count the number of HTTP requests for this page.
If you think about it, giving write access to your server system to anyone anywhere on the Internet is a very bad idea. It would be the equivalent of leaving the keys in your car with the engine running and a sign on the windshield saying, "Help yourself." One of Java's goals is to improve, not undermine, system security and so there is no built-in support allowing an applet to directly write to a file on a server. None! You can read a file on the server inside an applet, but you absolutely cannot directly create or write a file on the server unless the server does it for you. But all is not lost. We could write the ping program even though Java doesn't support ICMP.
How can you persuade the server to write a file for you? One possibility is to write a system demon that runs on the server 24 hours a day and listens to a specified port. When one of your applets wants to write a file, it opens a socket on that port on the server, sends commands saying what it wants to do, gives the filename, and then sends the data. This is open to abuse, though. Anyone who knows about this service could similarly connect to that port and fill up your file system with their files. You could lessen the probability of that happening if you protected the service by a password challenge.
The Linlyn Class for File Transfer
Well, what do you know? The previous paragraph is a pretty good description of the standard FTP (file transfer protocol) service. This service will frequently be running on any system that has full TCP/IP support. The standard FTP port is port 21 for commands and port 20 for data. Simply open a socket connection from your applet to port 21 on the server and you can have FTP write the files for you. Of course, you have to know the FTP commands to send, and you have to be able to deal with the password challenge.
Luckily, that work has already been done for you. The request for code to write to the server from an applet is made so frequently that I got together with my colleague, Bob Lynch, and we created the Linlyn class. (Hey, at least the name's not an acronym.) The Linlyn class does easy FTP file transfer from an applet to a server. The Linlyn class was designed with absolute ease-of-use as its foremost consideration, and a copy of it (along with a sample applet) is on the CD in the goodies directory.
The Linlyn class is published under the GNU public license for anyone to use or improve. Be advised that in its standard form you compile the userid and password as a String into the class file. That is appropriate only for use on a secure intranet, and must not be used for an applet that you publish on the Internet. It would be a simple matter, however, for you to prompt the user for the password, instead of hard-coding it in the program. Then you or anyone that you trust with your FTP password (and your credit card, car keys, wallet, etc.) can write files on your server from your applet.