To save space, I removed the annotation added by the interpreter
Out.write ("/r/n");
The previous sentence was due to a line break generated after the <%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
Out.write ("<!--documents start-->");
Out.write ("/r/n
Out.print ("Output");
Out.write ("/r/n</body>/r/n
catch (Throwable t) {
if (out!= null && out.getbuffersize ()!= 0)
Out.clearbuffer ();
if (PageContext!= null) pagecontext.handlepageexception (t);
finally {
if (_jspxfactory!= null) _jspxfactory.releasepagecontext (PageContext);
}
}
}
From the above code can clearly see the JSP built several objects (out, request, response, session, PageContext, Application, config, page) is how to produce, A friend who knows the servlet can see it.
The following is an important understanding of the Out object, which is declared as a jspwriter type, and JspWriter is an abstract class that can be found in the package javax.servlet.jsp.
Abstract public class Javax.servlet.jsp.JspWriter extends java.io.writer{
Abstract public void newline () throws IOException;
Abstract public void Print (Boolean arg0) throws IOException;
Abstract public void print (char arg0) throws IOException;
Abstract public void print (int arg0) throws IOException;
Abstract public void print (long arg0) throws IOException;
Abstract public void print (float arg0) throws IOException;
Abstract public void print (double arg0) throws IOException;
Abstract public void print (Char<> arg0) throws IOException;
Abstract public void print (String arg0) throws IOException;
Abstract public void print (Object arg0) throws IOException;
Abstract public void println () throws IOException;
Abstract public void println (Boolean arg0) throws IOException;
Abstract public void println (char arg0) throws IOException;
Abstract public void println (int arg0) throws IOException;
Abstract public void println (long arg0) throws IOException;
Abstract public void println (float arg0) throws IOException;
Abstract public void println (double arg0) throws IOException;
Abstract public void println (Char<> arg0) throws IOException;
Abstract public void println (String arg0) throws IOException;
Abtract public void println (Object arg0) throws IOException;
Abstract public void Clear () throws IOException;
Abstract public void Clearbuffer () throws IOException;
Abstract public void flush () throws IOException;
Abstract public void Close () throws IOException;
public int getbuffersize ();
Abstract public int getremaining ();
public boolean isautoflush ();
}
I believe that when I write here you may already know what I want to do. Yes, a steal, inherits the JspWriter class, then implements its defined virtual function, and then replaces the out variable with an instance of your own implementation class. OK.
JspWriter Out_bak = out;
String arg1= "Argument1";
String FilePath = "/cache/to generate filename _" + arg1 + ". html";
First to determine whether the file already exists, if it does not exist to execute this page, otherwise jump to the static page OK
File F = new file (Pagecontext.getservletcontext (). Getrealpath (FilePath));
if (F.exists ())
{
Out_bak.clear ();
Pagecontext.forward (FilePath);
System.out.println ("Go straight to Static page");
Return
}
out = new Htmlintofile (Pagecontext.getservletcontext (). Getrealpath (FilePath));
Out.write ("<!--documents start-->");
%>
<body>
<%= "See, this is the output is redirected to the implementation of the file, very simple ^_^"%>
</body>
<%
Out.close ();//Close the generated static file
Out_bak.clear ();
Pagecontext.forward (FilePath);
System.out.println ("Go to static page after executing this page");
Return
%>
3. Update issues
Here is a discussion of how to update the generation of static files, in fact, from the above implementation you can see, very simple is to create static files can be deleted, as to when to delete, depends on your needs. I can think of several situations as follows
When you use to generate data updates for a page
If you do not need to provide time for the data can be regularly updated
Never update
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.