关于applet写入文件的处理

发布时间:2024-02-19 11:28:51
javaduke原创wwwwwww.dukejava.com ----众所周知,娇娃网上家园applet写入文件的处理是安全考虑,applet在ie、没有能力在netscape中写入文件,包括本地和服务器文件。那么,我们如何保留一些数据呢?(在本程序代码中记录积分的功能)。一种方法是通过服务器端的servlet写入文件,applet端代码如下:private void 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(chatURL,queryString)).openConnection();showStatus("打开连接");showStatus(connect.toString ());System.out.println("open connection");connect.setDefaultUseCaches(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("试着连接和读取回应");InputStreamReader reader=new InputStreamReader(connect.getInputStream ());BufferedReader in=new BufferedReader(reader);message=in.readLine ();while(message!=null){tf.setText(message);message=in.readLine ();}showStatus("对话成功");}catch(MalformedURLException 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端代码如下!Servlet写入文件(www.mycgiserver.com调试成功)import javax.servlet.http.*;import java.io.*;import java.util.*;public class SaveServlet extends HttpServlet {String counter,counter2;//Initialize global variablespublic void init(ServletConfig config) throws ServletException {super.init(config);}//Service the requestpublic void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {File file=new File("/members/YourID/servlet/gamelog.txt");RandomAccessFile raf;if(!file.exists()){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 informationpublic String getServletInfo() {return "game.SaveServlet Information";}}
上一篇 java小程序中鼠标事件的响应处理(包含右键、双击等)
下一篇 Jini - Java 的實際應用

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题