Results 1 to 19 of 19
  1. #1
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default Modify content of a file on server using an applet

    Hi,
    I'm trying to use an applet to modify content of a text file on my server, by using the following code (originally an example from here, with some change), but when I run the applet in a browser, the text file remains unchanged. The ACL of the text file is set to be 777. Any advice is appreciated!

    Java Code:
    import java.applet.Applet;
    
    import java.io.*;
    import java.net.*;
    
    public class test2 extends Applet{
    
    public void init() {
    
    StringBuffer buf = new StringBuffer();
    try {
    
    	String output = "this is some string";
    	
    	URL aUrl = new URL (getCodeBase().toString() + "test.txt");
    	
    	System.out.println(aUrl);
    	URLConnection con = aUrl.openConnection();
    	con.setDoOutput(true);
    	PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(con.getOutputStream())));
    	out.println(output);
    	out.close();
    }
    catch (MalformedURLException mue) {
    	System.out.println("improper url");
    	mue.printStackTrace();
    }
    catch (IOException e) {
    	System.out.println("io exception");
    	e.printStackTrace();
    }
    }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What code do you have on the server that is going to do the update requested by the applet?
    What protocol does the server use?
    The applet can send a message to the server it was loaded from and tell the server to do something.

  3. #3
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    My code is very simple, just trying to load the applet.

    As of protocol, I use a remote log-in to access the server, if that is what you are asking.

    Java Code:
    <html>
    <head>
            <title> test page </title>
    </head>
    
    <body>
    <h1>Test page</h1>
    
    <applet code="test2.class" width="600" height="415"></applet>
    
    </body>
    </html>

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What does the server do when it receives the data written by your applet?
    Is there a log or debug mode where you can see what the server does?
    Is this server on the internet or is it on a local net?

  5. #5
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    The server is on internet, I tried to google for log file or debug mode, but no luck yet.
    Is there another way to do it?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    In most cases the people in charge of a server want to control what is written on the server's disks.
    When you connect to a server with a URL connection and send something to the server, what do you expect the server to do with the data it gets?
    That's where protocols come in. For example HTTP and FTP.
    Those protocols have ways to send commands and data to a server.

    What kind of server are you using? What protocols does it support?

  7. #7
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    I used another server, and I found a log file on it. Here I paste most recent records (records after I opened foo.htm which runs the applet):

    "
    - - [03/Jul/2011:16:12:27 -0400] "GET /foo.htm HTTP/1.1" 200 160 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
    - - [03/Jul/2011:16:12:30 -0400] "GET /test2.class HTTP/1.1" 200 1454 "-" "Mozilla/4.0 (Windows 7 6.1) Java/1.6.0_26"
    - - [03/Jul/2011:16:12:31 -0400] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
    - - [03/Jul/2011:16:12:31 -0400] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
    - - [03/Jul/2011:16:12:31 -0400] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
    - - [03/Jul/2011:16:15:49 -0400] "GET /foo.htm HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
    - - [03/Jul/2011:16:16:05 -0400] "GET /foo.htm HTTP/1.1" 200 160 "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"
    - - [03/Jul/2011:16:16:15 -0400] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"
    - - [03/Jul/2011:16:16:20 -0400] "GET /test2.class HTTP/1.1" 304 - "-" "Mozilla/4.0 (Windows 7 6.1) Java/1.6.0_26"

    Can you figure something out of this? Thanks.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    GET /test2.class
    The last line was from when the browser asked for the test2.class file that was referenced in the html file.

    Are there any messages in the browser's Java console?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Somewhere it the menu items there is a choice to view it.
    In the Control Panel there is a java shortcut that will allow you to set it on always.

  10. #10
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    Thanks, the java console is working, the only thing it outputs is the location of the text.txt file. Since I have this line in the souce code:
    Java Code:
    System.out.println(aUrl);

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    The server looks like it uses HTTP.
    Does it have a way for you to add your own code to it so your applet can send an HTTP request to it?
    Then your code on the server could write the file there.

  12. #12
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    What code do I need to add?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    It depends on the server's capabilities.
    Why not use FTP? It's already defined.

  14. #14
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    Which class provides FTP? If possible can you give an example? Thanks.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Do a Google. I think Apache has some classes to do it.

  16. #16
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    Oh, basically I need to write an java applet to update content of a file on the server, that's why I'm doing it in this way. But it doesn't work. Do you know any other way I can do it using java applet? Thanks.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Use a protocol to send the file and instructions to the server telling the server what to do with the file.
    FTP has commands to do that. The applet can use the FTP protocol to tell the server to save the file that the applet is sending

    How does the applet get the file it is supposed to send?

  18. #18
    calnastic is offline Member
    Join Date
    Jun 2011
    Posts
    18
    Rep Power
    0

    Default

    It's not sending a new file, but rewrite the content of a file. Like it was "123" in test.txt, but I want to update it to "abc". However my code doesn't do it.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

Similar Threads

  1. Can an applet read a file on server?
    By calnastic in forum New To Java
    Replies: 3
    Last Post: 06-29-2011, 07:38 PM
  2. applet can't write file in web server??
    By rajula in forum Java Applets
    Replies: 1
    Last Post: 05-02-2011, 03:48 PM
  3. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  4. [SOLVED] Applet reading from file on server
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-27-2009, 12:33 AM
  5. Replies: 2
    Last Post: 10-05-2008, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •