Java Applet I/O



 
Applet Standard Output
 
 
  • when you invoke System.out.println(....);  the output will go to the Java Console Window.  This is a separate window you can bring up from the browser control interface.
  • Used for copyright notices or you can use for debugging

 
 
File I/O Restrictions
  • cannot read or write from the disk on the machine where the browser is running.  (unless you request  and are granted permission)
  • You can read or write (theoretically) to files on the system where the applet is originally stored. 
  • you cannot read/write to other remote servers (unless you request and are granted permission)
    •  
       
    The steps
    • Unlike File I/O in a application that is doing file I/O with the machine it is running on, you can be default only do file I/O with the Applet Server.  Hence you need to do file I/O with a remote server.   As a consequence, getting the stream involves a few additional steps using the java.net package:

     

     

     

    Applet File Input: Reading  


     1)Create an instance of URL pointing to the url of the file
     2)Create an instance of URLConnection class by calling the
        openConnection() method of your URL instance from step 1
     3)Create an instance of InputStream class associated with your
        URLConnection object of step 2 by calling its method 
        getInputStream()    (see below for more details on writing)

     Example Code for File Reading 
     Example Applet Reading a File (bring up Java Console to see results) 
     Data File did reading from 
       

    Applet Output: Writing

    Reading is easy, but to write, the server must support your connection. What does this mean? Well, the URL we used to read data from a file above used the scheme HTTP. But HTPP is a protocol that only allows downloading (i.e. reading), but, not uploading (i.e. writing). So, we can't use this here.

    There are 3 Solutions to accomplish writing to a file on the server the Applet resides on.

    • Solution 1 = The first solution is to use a protocol like FTP for your URL as it does support uploading (i.e. writing). But, there are some bugs with specifying a non-anonymous ftp URL as well as other problems. So, while I have given you the code below, it doesn't work..but, they are attempting to add this to the next version of Java. Note: the fix to these bugs is to compile with version 1.3 and have a browser who's JVM is 1.3 standard!!!!!!

    So, the are 2 othere solutions, will support uploading on the server via:

    • Solution 2 = server has CGI application that can connect to via http URL and send information to it. The CGI application runs on the server and then writes the data for the Applet to the data file.
    • Solution 3 = Forget using the URL class which has trouble supporting non-anonymous ftp connections, and instead use the alternative Socket class in java.net to form a FTP-based connection to your datafile.

     

    Solution Synopsis

    Solution 1

    • does not work on our Newton server with most Browsers due to Bug in Java.
    • directly open up a connection to the data file on the server using URL, and URLConnection from Applet.
    • Note: URL should be made with an uploading scheme like ftp and not http.

    Solution 2

    • works only with Internet Explorer on our Newton server.
    • Reason does not work on Netscape is unknown but, is a reported bug at java.sun.com
    • open up a connection to a separate Server Application (e.g. Perl script) on the server using URL, and URLConnection from Applet. Via writes send data to Server Application. Server Application actually performs the output to a data file on the server

    Solution 3

    • works with both I.E and Netscape on our Newton server
    • Use Socket class instead of URL and URLConnection to open a connection to write using FTP to the data file on the server from the Applet.

     

     

    OUR SITUATION FOR NEWTON SERVER:

    Newton will work with IE browser via Solution 2 and both browsers via Solution 3.

    SOLUTION 1

    Directly from Applet

    SOLUTION 2

    Using a SEPARATE Server Application in Perl to Perform Writing:

    Idea:

    • As in the previous example of file reading, open connection to data file directly from Applet using URL and URLConnection.
    • Note: URL should be made with an uploading scheme like ftp and not http.
    • NOTE: There are a number of reported bugs (search on ftp URL at javasoft.com ) with creating a URL with ftp using a login and password....hence this example uses http. BUT, it should use ftp for uploading. Because of bugs, WILL NOT WORK!!!! (need latest version 1.3 for supposed fix....but, your browser's JVM should also be of this version).

     

    Idea:

    • One way of working around access restrictions is to use a separate server application that executes on the applet's host. Thus, even with these firewall, etc. restrictions on writing, you can use the server application to recieve the data and write to the file. This application can be written in Java or any other language like Perl. In the case of Java you need to actually, use sockets to communicate with it. In the case of Perl, it is a bit easier.

     

    Process:

    1. Open up direct outputstream to the datafile from Applet thru a URLConnection.
    2. Writes out data using this outputstream.

     

    Process:

    1. Opens up an outputstream to a separate Server Application from Applet thru a URLConnection. Server Application, here in Perl must be on host machine Applet resides on.
    2. Server Application is actually responsible for recieving output from the Applet and then parsing this output and writing the information to a data file on the server.
    3. "Send" information to the Server Application from the Applet by writing out data to outputstream of step 1...that is connected to the Server Application.

    EXAMPLE

     

     

    EXAMPLE

    Solution 3:

    Directly from Applet using Sockets and FTP

    Idea:

    • Another way of working around access restrictions is to use the Socket class in java.net package instead of URL and URLConnection. By opening a Socket that uses the FTP protocol we can transfer data to the file.
    • This requires the Applet web-server is also an ftp-server.
    • The data file is located in a directory where you have ftp access (and you know the login and password for an account which can access it).

    Process:

    1. The solution code below uses 2 classes, but you could do this all inside of a single class if desired.
    2. The applet is really simple and creates an instance of a class called LinLyn that was retrieved from the internet at http://www.afu.com/jdownload.html
    3. This class LinLyn has comments in the header showing how to use it. It uses Sockets to open connections to the ftp server (at the port number referenced by the class variable CNTRL_PORT ). See the code for details.

    EXAMPLE


     

     

     

    POTENTIAL Problem with Applet Writing to a File on Host Server

    Note: Depending on the networking environment an applet is loaded into, and depending on the browser that runs the applet, an applet might not be able to communicate with its originating host. For example, browsers running on hosts inside firewalls often cannot get much information about the world outside the firewall. As a result, some browsers might not allow applet communication to hosts outside the firewall. This restriction can be only on writing and not on reading files.