JSP file operations are an important part of website programming. Asp has discussed a lot about file operations. Let's take a look at how JSP files are implemented.
Compile file. After compiling the JavaBean file, place the class file in the corresponding class directory (tomcat environment ).
With the method of reading and writing files in JSP, it is not difficult to make a simple counter. You can try it :)
◆ JSP file operation: WriteOver. Jsp
- <Html>
- <Head>
- <Title> write a file </title>
- </Head>
- <BodyBgcolor="#000000">
- <%-- Create JavaBean and set properties -- %>
- <Jsp: useBeanId="Writer" Class="WriteOver" Scope="Request">
- <Jsp: setPropertyName="Writer" Property="Path" Value="/Test/afile.txt"/>
- <Jsp: setPropertyName="Writer" Property="Something" Value="Initializing somthing attributes"/>
- </Jsp: useBean>
-
- <H3> write a file
-
- <P>
- <%-- Set the string to be written -- %>
- <% Writer. setSomething ("writing something to a file"); %>
- <%-- Read the string set above -- %>
- <% Out. print (writer. getSomething (); %>
- <%-- Call writer's writeSomething method to write data to the file and return the 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; // file path
- Private String something; // The Written String
- // Initialization
- Public WriteOver (){
- Path=Null;
- Something="Default text";
- }
-
- // Set the file path
- Public void setPath (String apath ){
- Path=Apath;
- }
-
- // Obtain the file path
- Public String getPath (){
- Return path;
- }
- // Obtain the string
- Public void setSomething (String asomething ){
- Something=Asomething;
- }
- // Set the string
- Public String getSomething (){
- Return something;
- }
-
◆ JSP file operation: Write a string to the file. If the operation succeeds, the success string is returned.
- 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();
- }
- }
- }
Do you know more about JSP file operations? We hope to help you with JSP file operations.
- What is JSP and its strong weakness?
- How Tomcat improves performance on JSP pages
- Use of scripts Based on JSP tutorials
- JSP tutorial basics-HTML form demonstration
- JSP tutorials-basic articles-simple homepage Creation