欢迎光临
我们一直在努力

jsp文件操作之追加篇-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

jsp文件操作之追加篇(本站文章)
转载请注明来源
来源 jsp中国论坛 http://jspbbs.yeah.net

如何用jsp将数据追加到一个文件中,下面是实现方法

writeappend.jsp

<html>
<head>
<title>append a file</title>
</head>
<body bgcolor="#000000">

<jsp:usebean id="writer" class="writeappend" scope="request">
  <jsp:setproperty name="writer" property="path" value="/path/to/afile.txt" />
  <jsp:setproperty name="writer" property="something" value="something already set as a property in writeappend" />
</jsp:usebean>

<h3>write to the file</h3>

<p>

<% writer.setsomething("something to write to the file"); %>
<% out.print(writer.getsomething()); %>

<% out.print(writer.writesomething()); %>

</p>
</body>
</html>

writeappend.java
import java.io.*;

/**
* writeappend.java
* written by  morgan catlin email: mfcatlin@csclub2.stthomas.edu
*   august 19, 1999
*
* variables:
*   string path = path to file to write to (ie. /you/afile.txt)
*   string something = something to write to the file
*
* methods:
*   void setpath() = sets path
*   string getpath() = returns path
*   void setsomething() = sets something
*   string getsomething() = returns something
*   string writesomething() = writes something to the path,
*      returns a message that indicates success or failure(an exception)
*/

public class writeappend {
   
   private string path;
   private string something;
   
   public writeappend() {
      path = null;
      something = "default message";
   } // constructor writeappend
   
   /**
    * mutator for the path property
    */
   public void setpath(string apath) {
      path = apath;
   } // mutator setpath
   
   /**
    * accessor for the path property
    */
   public string getpath() {
      return path;
   } // accessor getpathclient
   
   /**
    * mutator for the something property
    */
   public void setsomething(string asomething) {
      something = asomething;
   } // mutator setsomething
   
   /**
    * accessor for the something property
    */
   public string getsomething() {
      return something;
   } // accessor getsomething
      
   /**
    * this method writes something to the path
    */
   public string writesomething() {
      try {
    
        filewriter thefile = new filewriter(path,true);
        printwriter out = new printwriter(thefile);
    out.print(something + "\n");
    out.close();
    thefile.close();
    return "das war sehr gut!";
      } catch (ioexception e) {
     return e.tostring();
      }    
   } // method writesomething
} // class writeappend
转载请注明来源
来源 jsp中国论坛 http://jspbbs.yeah.net

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp文件操作之追加篇-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章