A scheme to convert dynamic pages to static

Source: Internet
Author: User
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. Below is a JSP file test.jsp the Java file that is converted by Tomcat Test$jsp.java the following: 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 {   }   &NB Sp Public testoutredir$jsp () {    }       private static Boolean _jspx_inited = false;       public final void _jspx_init () throws Org.apache.jasper.runtime.JspException {  & nbsp }       public void _jspservice (HttpServletRequest request, httpservletresponse  Response) & nbsp;       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 is due to the subsequent line of                 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, you can clearly see several objects built in the JSP (out, request, response, Session, PageContext, Application, config, page) is how to produce, know the servlet's friends 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 ReplacementSuppose Out.close ();//Closes the generated static file Out_bak.clear ();p Agecontext.forward (FilePath); SYSTEM.OUT.PRINTLN (Run this page and go to static 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, data that is not required to provide time can be updated regularly and never updated.

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.