Aplet, serwer i plik

0

Cześć.
Muszę napisać aplet będący czymś w rodzaju księgi gości. Problem polega na zapisywaniu tekstu do pliku, który ma być na serwerze, gdyż teoretycznie nie jest to możliwe z poziomu apletu. Czy ktoś wie jak takie coś ominąć? Może chodzi o napisanie servletu? Jeśli ktoś mogłby zamieścić tutaj kawałek kodu odpowiedzialny za zapis danych do pliku na serwerze, to byłbym bardzo wdzięczny.

0

Serwery z obsługa servletów <url> są drogie i trudno dostepne (z tego co się orientuję).
Nie myślałeś o bazie danych na serwerze z którą się aplet po prostu łączy np ODBC ?</url>

0

W sumie to można i przez baze danych, ale znalazłem jeszcze jeden sposób - przez wysyłanie do serwera stringa przy uzyciu klasy URL i klasy TestPutFile. Nie moge jakoś jednak poskładać tych dwóch programów, aby całość działała. Tzn, mam: kod apletu, dwa programy ktore zamieszczam ponizej i co musze zrobic aby to współgrało?. Chodzi pewnie o to, aby wkleić peirwszy program do drugiego, ale podczas kompilacji wyskakuje błąd :(

import java.net.;
import java.io.
;

// This class provides a static method to post a file to the
// putfile script, which takes a filename as a parameter passed
// in the POST request itself, and then receives the bytes as
// the posted data.

public class PostPutFile extends Object
{
// Put sends the named file to a specific URL. The URL should
// contain the path name of the putfile script. This method
// will append the ?filename to the script name.
// It returns 0 if the put was successful, or a non-zero number
// if it failed for some reason.

public static int put(URL url, String filename, byte[] bytes)
throws IOException, MalformedURLException
{

// Run the putfile script and ask it to store the data in a file called "putme"

    URL destURL = new URL(url.getProtocol(), url.getHost(),
      url.getPort(), url.getFile()+"?"+filename);

// Define the data that you want stored in the file.

    URLConnection urlConn = destURL.openConnection();

    urlConn.setDoOutput(true);    // we need to write
    urlConn.setDoInput(true);    // just to be safe...
    urlConn.setUseCaches(false);    // get info fresh from server

// Tell the server what kind of data we are sending - in this case,
// just a stream of bytes.

    urlConn.setRequestProperty("Content-type",
        "application/octet-stream");

// Must tell the server the size of the data we are sending. This also
// tells the URLConnection class that we are doing a POST instead
// of a GET.

    urlConn.setRequestProperty("Content-length", ""+bytes.length);

// Open an output stream so we can send the info we are posting

    OutputStream outStream = urlConn.getOutputStream();

// Write out the actual request data

    outStream.write(bytes);

    outStream.close();

// Now that we have sent the data, open up an input stream and get
// the response back from the server

    DataInputStream inStream = new DataInputStream(
        urlConn.getInputStream());

    String line = inStream.readLine();

    inStream.close();

    try {
        int result = Integer.valueOf(line).intValue();
        return result;
    } catch (Exception parseError) {
        return -1;
    }
}

}

//------------------------------------------

import java.net.;
import java.applet.
;

public class TestPutFile extends Applet
{
public void init()
{
try {
URL destURL = new URL(getDocumentBase(),
"/cgi-bin/putfile");

// Define a string we want to send

           String dataToSend = "This is a string that I want \n"+
                "to store in the file.\n";

// The PostPutFile class wants a byte array, however, so we convert
// the string to a byte array.

           byte[] bytes = new byte[dataToSend.length()];

           dataToSend.getBytes(0, dataToSend.length(), bytes, 0);

           PostPutFile.put(destURL, "/home/mark/putme", bytes);

      } catch (Exception e) {
           e.printStackTrace();
      }
 }

}

0

Aha, i zapomnialbym że w kursie w którym znalazłem te programy było jeszcze coś takiego:

#!/bin/sh
echo "Content-type: text/plain"
echo
dd ibs=1 count=$CONTENT_LENGTH of=$QUERY_STRING 2>/dev/null
echo $?

This script probably requires a bit of explanation. First of all, the filename that you are storing into is encoded in the URL and not in the posted data. If you were to call this script putfile and put it in the cgi-bin directory of your Web server, the following URL would try to post to a file called putme:

http://mywebhost/cgi-bin/putfile?putme

Co to za język i czym to kompilować? Jakby mi ktoś opisał krok po kroku co mam zrobić z tymi programami byłbym naprawde wdzięczny.

Pozdrawiam.

PS. programy te pocchodzą z bardzo rozbudowanego kursu, który na pewno się Wam przyda podczas przygód z Javą: http://www.wutka.com/hackingjava/

0

to jest skrypt shella sh na jakims unixie/linuksie

0

kolejne rozwiazanie to wysylanie POSTem na serwer i np skrypt php, ktory to przechwyci i wrzuci do bazy
ale nie wiem czy sie oplaca... sa chyba lepsze rozwiazania do ksiegi gosci niz aplet Java

1 użytkowników online, w tym zalogowanych: 0, gości: 1