JSP file operations-file write operations

Source: Internet
Author: User

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

 
 
  1. <Html>
  2. <Head>
  3. <Title> write a file </title>
  4. </Head>
  5. <BodyBgcolor="#000000">
  6. <%-- Create JavaBean and set properties -- %>
  7. <Jsp: useBeanId="Writer" Class="WriteOver" Scope="Request">
  8. <Jsp: setPropertyName="Writer" Property="Path" Value="/Test/afile.txt"/>
  9. <Jsp: setPropertyName="Writer" Property="Something" Value="Initializing somthing attributes"/>
  10. </Jsp: useBean>
  11.  
  12. <H3> write a file
  13.  
  14. <P>
  15. <%-- Set the string to be written -- %>
  16. <% Writer. setSomething ("writing something to a file"); %>
  17. <%-- Read the string set above -- %>
  18. <% Out. print (writer. getSomething (); %>
  19. <%-- Call writer's writeSomething method to write data to the file and return the success or error message -- %>
  20. <% Out. print (writer. writeSomething (); %>
  21. </P>
  22. </Body>
  23. </Html>


◆ JSP file operation: WriteOver. java JavaBean file

 
 
  1. Import java. io .*;
  2.  
  3. Public class WriteOver {
  4.  
  5. Private String path; // file path
  6. Private String something; // The Written String
  7. // Initialization
  8. Public WriteOver (){
  9. Path=Null;
  10. Something="Default text";
  11. }
  12.  
  13. // Set the file path
  14. Public void setPath (String apath ){
  15. Path=Apath;
  16. }
  17.  
  18. // Obtain the file path
  19. Public String getPath (){
  20. Return path;
  21. }
  22. // Obtain the string
  23. Public void setSomething (String asomething ){
  24. Something=Asomething;
  25. }
  26. // Set the string
  27. Public String getSomething (){
  28. Return something;
  29. }
  30.  

◆ JSP file operation: Write a string to the file. If the operation succeeds, the success string is returned.

 
 
  1. public String writeSomething() {   
  2. try {   
  3.    
  4.  File f = new File(path);   
  5.  PrintWriter out = new PrintWriter(new FileWriter(f));   
  6.  out.print(this.getSomething() + "  
  7. ");   
  8.  out.close();   
  9.  return "Success.";   
  10. } catch (IOException e) {   
  11.  return e.toString();   
  12. }   
  13. }   
  14. }  

Do you know more about JSP file operations? We hope to help you with JSP file operations.

  1. What is JSP and its strong weakness?
  2. How Tomcat improves performance on JSP pages
  3. Use of scripts Based on JSP tutorials
  4. JSP tutorial basics-HTML form demonstration
  5. JSP tutorials-basic articles-simple homepage Creation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.