A scheme to convert dynamic pages to static under JSP

Source: Internet
Author: User
Tags abstract object config final return string
js| News | static | page

1. Foreword

In order to understand the origin of this framework, we first look at the content of the Java file that the JSP parser converts to our JSP code.

The following is a JSP file test.jsp

<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<%
Out.write ("<!--documents start-->");
%>
<body>
<%= "Output"%>
</body>

The contents of the Java file Test$jsp.java converted from Tomcat are as follows:

Package org.apache.jsp;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import javax.servlet.jsp.*;
Import org.apache.jasper.runtime.*;
public class Test$jsp extends Httpjspbase {
static {
}
Public testoutredir$jsp () {
}
private static Boolean _jspx_inited = false;
Public final void _jspx_init () throws Org.apache.jasper.runtime.JspException {
}
public void _jspservice (HttpServletRequest request, httpservletresponse response)
Throws Java.io.IOException, Servletexception {
Jspfactory _jspxfactory = null;
PageContext pagecontext = null;
HttpSession session = NULL;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
String _value = null;
try {
if (_jspx_inited = = False) {
Synchronized (this) {
if (_jspx_inited = = False) {
_jspx_init ();
_jspx_inited = true;
}
}
}
_jspxfactory = Jspfactory.getdefaultfactory ();
Response.setcontenttype ("text/html;charset=gb2312");
PageContext = _jspxfactory.getpagecontext (this, request, response,
"", True, 8192, true);
application = Pagecontext.getservletcontext ();
Config = Pagecontext.getservletconfig ();
Session = Pagecontext.getsession ();
out = Pagecontext.getout ();
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\nOut.print ("Output");
Out.write ("\r\n</body>\r\ncatch (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{
Final public static int no_buffer = 0;
Final public static int default_buffer =-1;
Final public static int unbounded_buffer =-2;
protected int buffersize;
protected Boolean AutoFlush;
protected Javax.servlet.jsp.JspWriter (int arg1, boolean arg2);
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.

  2. Implement replacement

Assume:

<%@ page language= "java"   contenttype= "text/html;charset=gb2312"
import= " Jwb.util.htmlintofile,jwb.util.tempsinglet,java.io.file "%><%
JspWriter out_bak = out;
String arg1= "Argument1";
String FilePath = "/cache/to generate filename _" + arg1 + ". html" from parameters;
//First determine if the file already exists, if it does not exist, execute this page, or 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, that's the implementation of the output being redirected to the file, it's easy ^_^"%>
</ Body>
<%
Out.close ();//Close the generated static file
Out_bak.clear ();
Pagecontext.forward (FilePath);
System.out.println ("Go to a static page after this page is executed");
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



Related Article

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.