Javaduke Originally in www.dukejava.com ---- 娃 网 home
About Applet write file processing
All all are known in the security considerations, Applet does not write to files in IE, Netscape, including local, server files. So how do we achieve some data reservations? (The function of logging points in this program code).
One method is to write files through the server-side servlet, the code of the Applet is as follows:
Private vid send () {
Message = score "#" tf.gettext ();
ShowStatus ("Message Send");
String queryString = "/ servlet / javaduke.servlet.saveservlet? Message ="
Urlencoder.Encode (Message);
p ("Attempting to Send:" Message);
Try {
Connect = (New URL (queryString). OpenConnection ();
ShowStatus ("Open Connection");
Showstatus (connection.toString ());
System.out.println ("Open Connection");
Connect.SetDefault (false);
Connect.SetuseCaches (false);
Connect.SetDoinput (TRUE);
Connect.SetDoOutput (false);
System.out.println ("DooutPut False");
Connect.Connect ();
System.out.println ("open stream");
P ("Made Connection TO" Connect);
ShowStatus ("Trying to connect, read response");
InputStreamReader Reader = New InputStreamReader (Connect.getInputStream ());
BufferedReader in = New BufferedReader (Reader);
Message = in.readline ();
While (Message! = null) {
Tf.Settext (Message);
Message = in.readline ();
}
Showstatus ("Dialogue Success");
}
Catch (Malformedurlexcection E2) {
System.err.Println ("Malformedurlexception!");
E2.PrintStackTrace (System.err);
ShowStatus ("Malformedurlexception");
}
Catch (IOException E1) {
System.err.Println ("IOException");
E1.PrintStackTrace (System.err);
Showstatus (e1.toString ());
}
}
Server end code is as follows!
Servlet write file (www.mycgiServer.com debugging successfully)
Import javax.servlet.http. *;
Import java.io. *;
Import java.util. *;
Public class saveservlet extends httpservlet {string counter, counter2;
// Initialize Global Variables
Public void init (servletconfig config) throws servletexception {
Super.init (config);
}
// service the request
Public void service (httpservletRequest request, httpservletResponse response) throws servletexception, ioException {
File file = new file ("/ members / Yourid / servlet / gamelog.txt");
Randomaccessfile Raf;
IF (! file.exiss ()) {
Raf = new randomaccessfile (File, "RW");
Counter = "210 # javaduke";
Raf.seek (0);
Raf.writebytes (counter);
Raf.close ();
}
Else {
Counter = Request.GetParameter ("Message");
Raf = new randomaccessfile (File, "RW");
Raf.seek (0);
Raf.writebytes (counter);
Raf.close ();
}
Response.setContentType (Text / HTML ");
PrintWriter out = new printwriter (response.getoutputstream ());
OUT.PRINT ("Saved OK!");
Out.close ();
}
// Get Servlet Information
Public string getServletInfo () {
Return "Game.sAvesRvlet Information";
}
}