How to implement File Writing Operation by JSP
This article mainly introduces JSP how to achieve file writing operation, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
JSP file manipulation is one of the important contents of website programming. Asp has discussed a lot about file manipulation. Let's take a look at how JSP files are implemented.
Two files are used here, one JSP file and one JavaBean file. You can easily write a text file by calling JavaBean in JSP. Please create a test directory to the web root directory, the program will create an afile.txt file, and the JavaBean file will be compiled and put the class file in the corresponding class directory (tomcat environment).
With the method of reading and writing files under JSP, to make a simple counter to believe that it is not a difficult thing, you can try:)
◆ JSP file operation: WriteOver.Jsp
< html > < head > < title > write a file < / title > < / head > < body bgcolor= "# 000000" > <%-- create JavaBean and set properties--% > < jsp:useBean id= "writer" class= "WriteOver" scope= "request" > < jsp:setProperty name= "writer" property= "path" value= "/ test/afile.txt" / > < jsp:setProperty name= "writer" property= "something" value= "initial Change the somthing attribute "/ > < / jsp:useBean > < h4 > write a file < / h4 > < p > <%-- set the string to be written--% > <% writer.setSomething (" write something to a file ") % > <%-- read the string set above--% > <% out.print (writer.getSomething ());% > <%-call the writeSomething method of writer to write to the file and return a success or error message--% > <% out.print (writer.writeSomething ());% > < / p > < / body > < / html >
◆ JSP file operation: WriteOver.java JavaBean file
Import java.io.*; public class WriteOver {private String path; / / string written by private String something;// initialize public WriteOver () {path = null; something = "default text";} / / set the file path public void setPath (String apath) {path = apath;} / / get the file path public String getPath () {return path } / / get the string public void setSomething (String asomething) {something = asomething;} / / set the string public String getSomething () {return something;}
◆ JSP file operation: writes a string to the file, and returns a success string if it succeeds
Public String writeSomething () {try {File f = new File (path); PrintWriter out = new PrintWriter (new FileWriter (f)); out.print (this.getSomething () + "); out.close (); return" Success. ";} catch (IOException e) {return e.toString () } Thank you for reading this article carefully. I hope the article "how to write documents in JSP" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!