A scheme to convert dynamic pages to static

Source: Internet
Author: User
Tags abstract config final
Dynamic | static | page 1. ForewordIn 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 (); %> <%= Output%> 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 last sentence is due to the <%@ page Language=java contenttype=text/html;charset=gb2312%> the following line Out . write (); Out . Write (\r\n\r\n\r\n\r\n); Out . Print (output); Out . Write (\r\n\r\n\r\n\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{ final public static intNo_buffer = 0; final public static intDefault_buffer =-1; final public static intunbounded_buffer =-2; protected intbuffersize; protected BooleanAutoFlush; protectedJavax.servlet.jsp.JspWriter ( intArg1, BooleanARG2); Abstract Public voidNewLine () throwsIOException; Abstract Public voidPrint BooleanARG0) throwsIOException; Abstract Public voidPrint CharARG0) throwsIOException; Abstract Public voidPrint intARG0) throwsIOException; Abstract Public voidPrint LongARG0) throwsIOException; Abstract Public voidPrint floatARG0) throwsIOException; Abstract Public voidPrint DoubleARG0) throwsIOException; Abstract Public voidPrint Char[] arg0) throwsIOException; Abstract Public voidPrint (String arg0) throwsIOException; Abstract Public voidPrint (Object arg0) throwsIOException; Abstract Public voidprintln () throwsIOException; Abstract Public voidprintln BooleanARG0) throwsIOException; Abstract Public voidprintln CharARG0) throwsIOException; Abstract Public voidprintln intARG0) throwsIOException; Abstract Public voidprintln LongARG0) throwsIOException; Abstract Public voidprintln floatARG0) throwsIOException; Abstract Public voidprintln DoubleARG0) throwsIOException; Abstract Public voidprintln Char[] arg0) throwsIOException; Abstract Public voidprintln (String arg0) throwsIOException; Abtract Public voidprintln (Object arg0) throwsIOException; Abstract Public voidClear () throwsIOException; Abstract Public voidClearbuffer () throwsIOException; Abstract Public voidFlush () throwsIOException; Abstract Public voidClose () throwsIOException; Public intGetBufferSize (); Abstract Public intGetremaining (); Public BooleanIsautoflush ();} 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 ReplacementAssume <%@ 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/generates the filename from the parameter _ + arg1 +. html; //First determine whether the file already exists, if it does not exist then 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 (); %><%= See, this is the output is redirected to the implementation of the file, very simple ^_^%><%out.close ();//Closes the generated static file out_bak.clear (); Pagecontext.forward (FilePath); System.out.println (run this page and go to static page);return;%> 3. Update issuesHere 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


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.