• Post Reply Bookmark Topic Watch Topic
  • New Topic

How the applet may write a file on the server?  RSS feed

Rui Tu
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a applet which can create a new file or change a file on the server. But because the applet security limitation, I can't do this by applet. So I want to get some addvice. Thank you very much!!!
Richard Harris
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd probably have to have the Applet communicate to something server side (i.e. a Servlet), and tell it to write the file.
One simple way would be to use the existing XML-RPC library for Java and define file and directory creation procedures that the Applet can call.
- Rick
Tim Holloway
Posts: 18552
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been a while since I've said it, but A WEB SERVER IS NOT A FILE SERVER!!!
It's not just the security issue, it's that the HTTP protocol doesn't have any provision for directly reading and writing files.
Yes, you need a servlet, but all that servlet has to do is take the data you post and write it out to a file using the standard Java file I/O services just like any other local application would.
One warning, however. Don't use relative file paths or attempt to use obscure J2EE services to attempt to figure out the "current directory". Many have tried, most have failed, and the rest are courting disaster, since there isn't any such concept in J2EE. Instead either hard-code the base directory path as an absolute path as part of your servlet (: or pick it up as a servlet startup parameter or property.
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to do the same:
i am a student and I have been trying to solve this with a Perl Script that is listed below. So, my appeal for help is as much academic as it is practical. So, please don't tell me about servlets or the magic of JSP. I want my Applet to talk! The Applet program is listed here (below) and this accesses a Perl cgi-bin script
on my localhost ( Apache server;-
#! C:\perl\bin\perl.exe
open(OUT, "> /public_html/opinion.txt");
print "content-type: text/plain\n\n";

while (<> {
print OUT $_;
print $_;
close (OUT);
exit 0;
# end of script

I'm a total beginner with Perl, so I don't have the first clue how to correct the Perl Script, if it is wrong. When I check the server log, it says it has accessed the script with the following message;-

"POST /cgi-bin/wdwrite.pl HTTP/1.1" 200 64
Here, also is the Applet. It's quite a simple progam;-
/** postMethod.java - see also wdwrite.pl
* Textfied and button. The button calls to a function writePage()
* this then gets the host() and opens a URL connection to send the
* data out, back to the place the Applet came with a dataOutputStream
* object and writeBytes() and linked back to a Perl cgi-bin program.
import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*; //Tidy these up later with .ActionEvent etc;
public class postMethod extends Applet implements ActionListener {
TextArea info;
Button Bsend;
String str;
String host, conext, exc1, exc2;
// init the textfield and button. Only the button needs ActionListener
public void init() {
info = new TextArea(5,68);
Bsend = new Button("SEND");
// show the results or exceptions in a series of strings
public void paint (Graphics g) {
g.drawString("Got this: "+str, 30, 150);// message 2B sent
g.drawString(host, 30, 170);// getHost().getCodeBase()
g.drawString(conext, 30, 190);// getConnection.toSting()
g.drawString(exc1, 30, 210);// didn't like the call to host
g.drawString(exc2, 30, 230);// didn't like the writeBytes()
// button links to the Post Method and calls the function writePage()
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Bsend) {
str = info.getText();
try {
writePage();// Has to be caught
catch (Exception e1) {
exc1 = "writePage: " + e1.toString();
repaint(); // exceptions /results etc
// writePage function returns to repaint. Links the page back to
// whence it came and directs it to the Perl script in cgi-bin
// Note: try-catch are not necessary here. ie: it should still work
// without this. The purpose is to catch an error message should
// an error occur. Sent as a String in repaint() g.drawString
public void writePage() throws Exception {
host = getCodeBase().getHost();
try {
URL url = new URL("http://"+host+"/cgi-bin/wdwrite.pl");
URLConnection con = url.openConnection();
conext = "connect OK: " + con.toString();
con.setRequestProperty("Content-type", "text/plain");
con.setRequestProperty("Content-length", str.length()+" ");

DataOutputStream out = new DataOutputStream(con.getOutputStream());
String content = URLEncoder.encode(str);
exc2 = "Seems OK: reads to final line.";
catch (Exception e2){
exc2 = "dataOutputStream: " + e2.toString();
}// end try-catch
}// end writePage()
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!