Results 1 to 19 of 19
- 07-03-2011, 08:16 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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(); } } }
- 07-03-2011, 08:26 PM #2
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.
- 07-03-2011, 08:52 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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>
- 07-03-2011, 08:56 PM #4
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?
- 07-03-2011, 10:02 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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?
- 07-03-2011, 10:07 PM #6
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?
- 07-03-2011, 10:17 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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.
- 07-03-2011, 10:19 PM #8
The last line was from when the browser asked for the test2.class file that was referenced in the html file.GET /test2.class
Are there any messages in the browser's Java console?
- 07-03-2011, 10:59 PM #9
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.
- 07-03-2011, 11:04 PM #10
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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);
- 07-03-2011, 11:09 PM #11
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.
- 07-03-2011, 11:13 PM #12
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
What code do I need to add?
- 07-03-2011, 11:15 PM #13
It depends on the server's capabilities.
Why not use FTP? It's already defined.
- 07-03-2011, 11:21 PM #14
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
Which class provides FTP? If possible can you give an example? Thanks.
- 07-03-2011, 11:24 PM #15
Do a Google. I think Apache has some classes to do it.
- 07-03-2011, 11:27 PM #16
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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.
- 07-03-2011, 11:55 PM #17
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?
- 07-04-2011, 01:33 AM #18
Member
- Join Date
- Jun 2011
- Posts
- 18
- Rep Power
- 0
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.
- 07-04-2011, 02:45 AM #19
Similar Threads
-
Can an applet read a file on server?
By calnastic in forum New To JavaReplies: 3Last Post: 06-29-2011, 06:38 PM -
applet can't write file in web server??
By rajula in forum Java AppletsReplies: 1Last Post: 05-02-2011, 02:48 PM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
[SOLVED] Applet reading from file on server
By DenniGa in forum Java AppletsReplies: 3Last Post: 02-26-2009, 11:33 PM -
Copy a .swf file from server side to client using signed applet
By Imoracle in forum Java AppletsReplies: 2Last Post: 10-05-2008, 06:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks