Under JSP, a program that implements a dynamic page to static

zhaozj2021-02-16  84

1. Foreword In order to understand the origin of this framework, we first learn about the JSP parser to convert the JSP code we write into the Java file. Here is a JSP file Test.jsp <% @ page language = "java" contenttype = "text / html; charset = GB2312"%> <% out.write ("");% > <% = "Output"%> The Java file Test $ jsp.java conversions through Tomcat is 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 comment out.write interpreter added ( "/ r / n"); // The previous sentence is due to <% @ page language = "java" contenttype = "text / html; charset = GB2312"%> Out.write (" "); out.write (" / r / n / r / n / r / n / r / n "); Out.print (" Output "); Out.write ("/ r / n / r / n / r / n");} catch (throwable t) {i (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 how the JSP built-in objects (Out, Request, Response, Session, Page, the friends who know the servlet can understand. The following focuses on the OUT object, which is declared as a JSPWRITER type, JSPWriter is an abstract class, which 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 oException;

Abstract Public Void Print (Boolean Arg0) Throws oException; Abstract Public Void Print (Char arg0) Throws oException;

Abstract Public Void Print (int Arg0) throws oewception;

Abstract Public Void Print (long arg0) throws oException;

Abstract public void print (Float arg0) throws ioException;

Abstract Public Void Print (double arg0) throws ioException;

Abstract Public Void Print (char [] arg0) throws oException;

Abstract public void print (string arg0) throws oException;

Abstract Public Void Print (Object Arg0) Throws oException;

Abstract public voidprintln () throws oException;

Abstract Public Void Println (Boolean Arg0) Throws oException;

Abstract public void println (char arg0) throws oException;

Abstract public void println (int Arg0) throws ioException;

Abstract public void println (long arg0) throws oException;

Abstract public void println (float arg0) throws oewception;

Abstract public void println (double arg0) throws oException;

Abstract public void println (char [] arg0) throws oException;

Abstract public void println (string arg0) throws oException;

Abtract Public Void Println (Object Arg0) Throws oException;

Abstract public void clear () throws oException;

Abstract public void clearbuffer () throws oException;

Abstract public void flush () throws oException;

Abstract public void close () throws oException;

Public int getBuffersize ();

Abstract public int getRemaining ();

Public boolean isautoflush ();

} I believe that when I wrote here, you may already know what I want to do. Yes, come to a stealing day, inherit the JSPWriter class, then implement its defined virtual function, then replace the OUT variable to the instance of the class yourself, it is OK. 2. Implement replacement

Suppose <% @ page language = "java" contenttype = "text / html; charset = GB2312" import = "jwb.util.htmlintofile, jwb.util.tempsinglet, java.io.temp"%> <% jspwriter out_bak = OUT String arg1 = "argument1"; string filepath = "/ cache / according to parameter generation file name _" arg1 ".html"; // first determine if the file already exists, if there is no existence, execute this page, otherwise jump When the static page is OK, file f = new file (pageconText.getServletContext (). GetRealPath (F.Exists ()) {out_bak.clear (); pageContext.Forward (filepath); system.out. Println ("Go directly to static page"); return;} out = new htmlintofile (pagecontext.getServletContext (). getRealPath (filepath)); out.write ("");%> <% = ", this is the implementation of the output being redirected to the file, very simple, ^ _ ^"%> <% Out.close (); // Close the generated static file out_bak.clear (); pageContext.Forward (filepath); system.out.println ("Go to Static Page after this page"); Return;%> 3 Let's discuss how to update how to generate a static file, in fact, you can see from above, very simple is to delete the generated static file, as for when to delete, you have to see your needs. Several conditions I can think of when the data update is used to generate a page

If you don't need to provide time-out data, you can update it.

Never update

pending upgrade……

转载请注明原文地址:https://www.9cbs.com/read-13864.html

New Post(0)