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.
- <% @ PageLanguage="Java" ContentType="Text/html; charset = UTF-8" PageEncoding=UTF-8"%>
- <% @ PageImport="Java. io .*"%>
- <%
- // Dynamically generate jsp files and then call
- StringFilename="TestDynamicInclude. inc. jsp";
- StringPath=Application. GetRealPath (".");
- FileWriterFw=NewFileWriter (path + "/" + filename );
- Fw. write ("<"+" % @ PageLanguage= \ "Java \"ContentType= \ "Text/html;Charset=UTF-8 \"PageEncoding= \ "UTF-8 \" % "+">");
- Fw. write ("<"+" % = New java. util. Date () % "+">");
- Fw. close ();
- %>
- <Jsp: include Page="<% = Filename %>" />
- <%
- // After the task is completed, you can delete the file.
- FileF=NewFile (path + "/" + filename );
- If (f. exists ()){
- F. delete ();
- }
- %>
- <% @ PageLanguage="Java" ContentType="Text/html; charset = UTF-8" PageEncoding=UTF-8"%>
- <% @ PageImport="Java. io .*"%>
- <%
- // Dynamically generate jsp files and then call
- StringFilename="TestDynamicInclude. inc. jsp";
- StringPath=Application. GetRealPath (".");
- FileWriterFw=NewFileWriter (path + "/" + filename );
- Fw. write ("<"+" % @ PageLanguage= \ "Java \"ContentType= \ "Text/html;Charset=UTF-8 \"PageEncoding= \ "UTF-8 \" % "+">");
- Fw. write ("<"+" % = New java. util. Date () % "+">");
- Fw. close ();
- %>
- <Jsp: include Page="<% = Filename %>" />
- <%
- // After the task is completed, you can delete the file.
- FileF=NewFile (path + "/" + filename );
- If (f. exists ()){
- F. delete ();
- }
- %>
-
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 () %>
- JSTL introduction-New JSP programming components allow tag Programming
- Eclipse plug-ins required for JSP development and installation and configuration methods
- Use JSP to implement simple SQL reports
- Use JSP to store and display database images
- JDBC and JSP simulate the MVC three-layer architecture