Dynamically generate and call JSP programs in JSP

Source: Internet
Author: User

The following JSP program dynamically generates a testDynamicInclude. inc. jsp file and calls it later.

In the dynamic generation of JSP, pay attention to the path in it. "." Represents the root directory of your project.

If your JSP file is under the test subdirectory, do not forget to add this path to the path.

 
 
  1. <% @ PageLanguage="Java" ContentType="Text/html; charset = UTF-8" PageEncoding=UTF-8"%>
  2. <% @ PageImport="Java. io .*"%>
  3. <%
  4. // Dynamically generate jsp files and then call
  5. StringFilename="TestDynamicInclude. inc. jsp";
  6. StringPath=Application. GetRealPath (".");
  7. FileWriterFw=NewFileWriter (path + "/" + filename );
  8. Fw. write ("<"+" % @ PageLanguage= \ "Java \"ContentType= \ "Text/html;Charset=UTF-8 \"PageEncoding= \ "UTF-8 \" % "+">");
  9. Fw. write ("<"+" % = New java. util. Date () % "+">");
  10. Fw. close ();
  11. %>
  12. <Jsp: include Page="<% = Filename %>" />
  13. <%
  14. // After the task is completed, you can delete the file.
  15. FileF=NewFile (path + "/" + filename );
  16. If (f. exists ()){
  17. F. delete ();
  18. }
  19. %>
  20. <% @ PageLanguage="Java" ContentType="Text/html; charset = UTF-8" PageEncoding=UTF-8"%> 
  21. <% @ PageImport="Java. io .*"%> 
  22. <%
  23. // Dynamically generate jsp files and then call
  24. StringFilename="TestDynamicInclude. inc. jsp";
  25. StringPath=Application. GetRealPath (".");
  26. FileWriterFw=NewFileWriter (path + "/" + filename );
  27. Fw. write ("<"+" % @ PageLanguage= \ "Java \"ContentType= \ "Text/html;Charset=UTF-8 \"PageEncoding= \ "UTF-8 \" % "+">");
  28. Fw. write ("<"+" % = New java. util. Date () % "+">");
  29. Fw. close ();
  30. %> 
  31. <Jsp: include Page="<% = Filename %>" /> 
  32. <%
  33. // After the task is completed, you can delete the file.
  34. FileF=NewFile (path + "/" + filename );
  35. If (f. exists ()){
  36. F. delete ();
  37. }
  38. %> 
  39.  

Warning for formal applications, files must be generated based on thread numbers to avoid competition among multiple threads, such as using an incremental sequence or a non-repeating sequence number generator.
String filename = "testDynamicInclude. inc" + seqNo + ". jsp ";

The content of the file dynamically generated by JSP is as follows:

<% @ PageLanguage="Java" ContentType="Text/html; charset = UTF-8" PageEncoding=UTF-8"%><% = New java. util. Date () %> 

  1. JSTL introduction-New JSP programming components allow tag Programming
  2. Eclipse plug-ins required for JSP development and installation and configuration methods
  3. Use JSP to implement simple SQL reports
  4. Use JSP to store and display database images
  5. JDBC and JSP simulate the MVC three-layer architecture

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.