Method of Realizing Dynamic Page to Static Page by JSP Technology

  • 2021-11-30 01:04:07
  • OfStack

This paper introduces the method of jsp technology to realize dynamic page to static page, and shares it with you as follows:

For the scheme of JSP technology to realize dynamic page to static page, we explain it from three steps:

JSP Technology to Realize Dynamic Page to Static Page Scheme 1:

In order to understand the origin of this framework in a simple way, we first understand the contents of JAVA file converted from JSP code written by JSP parser.

The following is an JSP file test. jsp


 < %@ page language=java contentType=text/html;charset=GB2312 % >  
 < % 
out.write( < !-- File start -- > ); 
% >  
 < html >  
 < head >  
 < body >  
 < %= Output % >  
 < /body >  
 < /head >  
 < /html >  

The Java file test $jsp. java converted from Tomcat reads 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 deleted the comments added by the interpreter  
 
        out.write(\r\n); 
 
 // Upper 1 Sentence is due to  
  < %@ page language=java contentType=text/html;charset=GB2312 % > the following line break  
 
        out.write( < !-- File start -- > ); 
 
        out.write(\r\n < html > \r\n < head > \r\n < body > \r\n); 
 
        out.print( Output ); 
 
        out.write(\r\n < /body > \r\n < /head > \r\n < /html > \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, we can clearly see how several built-in objects of JSP (out, request, response, session, pageContext, application, config, page) come into being, and friends who know servlet can understand it by looking at it.

Let's focus on understanding the out object under 1, which is declared as JspWriter type, and JspWriter is an abstract class, whose definition can be found in the package javax. servlet. jsp.


abstractpublicclassjavax.servlet.jsp.JspWriterextends 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); 
 
    
 
  abstractpublicvoidnewLine()throwsIOException; 
 
  abstractpublicvoidprint(booleanarg0)throwsIOException; 
 
  abstractpublicvoidprint(chararg0)throwsIOException; 
 
  abstractpublicvoidprint(intarg0)throwsIOException; 
 
  abstractpublicvoidprint(longarg0)throwsIOException; 
 
  abstractpublicvoidprint(floatarg0)throwsIOException; 
 
  abstractpublicvoidprint(doublearg0)throwsIOException; 
 
  abstractpublicvoidprint(char[]arg0)throwsIOException; 
 
  abstractpublicvoidprint(Stringarg0)throwsIOException; 
 
  abstractpublicvoidprint(Objectarg0)throwsIOException; 
 
  abstractpublicvoidprintln()throwsIOException; 
 
  abstractpublicvoidprintln(booleanarg0)throwsIOException; 
 
  abstractpublicvoidprintln(chararg0)throwsIOException; 
 
  abstractpublicvoidprintln(intarg0)throwsIOException; 
 
  abstractpublicvoidprintln(longarg0)throwsIOException; 
 
  abstractpublicvoidprintln(floatarg0)throwsIOException; 
 
  abstractpublicvoidprintln(doublearg0)throwsIOException; 
 
  abstractpublicvoidprintln(char[]arg0)throwsIOException; 
 
  abstractpublicvoidprintln(Stringarg0)throwsIOException; 
 
  abtractpublicvoidprintln(Objectarg0)throwsIOException; 
 
  abstractpublicvoidclear()throwsIOException; 
 
  abstractpublicvoidclearBuffer()throwsIOException; 
 
  abstractpublicvoidflush()throwsIOException; 
 
  abstractpublicvoidclose()throwsIOException; 
 
  publicintgetBufferSize() ; 
 
  abstractpublicintgetRemaining(); 
 
  publicbooleanisAutoFlush(); 
 
} 

I believe you may already know how to do it here. Yes, for a change of day, inherit the JspWriter class, then implement its defined virtual function, and then replace the out variable with your own implementation of the class instance ok.

Solution of JSP Technology to Realize Dynamic Page to Static Page 2:

Realize substitution

Hypothesis


 < %@ page language=java contentType=text/html;charset=GB2312 import=jwb.util.HtmlIntoFile,jwb.util.TempSinglet,java.io.File% >  
 < % 
 
JspWriter outout_bak =out;String arg1=argument1;String filePath = /cache/ Generate file name according to parameters _ + arg1 + .html; 
 
// First, judge whether the file already exists. If it does not exist, execute this page, otherwise jump to the static page OK It's over File f = new File(pageContext.getServletContext().getRealPath(filePath)); 
 
if(f.exists()){ out_bak.clear(); pageContext.forward(filePath); System.out.println( Go directly to the static page ); 
 
 return;}out= new HtmlIntoFile(pageContext.getServletContext().getRealPath(filePath));out.write( < !-- File start -- > ); 
% >  
 < html >  
 < head >  
 < body >  
 < %=  See, this is the implementation of redirecting the output to a file, which is very simple, isn't it ^_^% >  
 < /body >  
 < /head >  
 < /html >  
 < % 
out.close(); 
 
// Close the generated static file out_bak.clear();pageContext.forward(filePath); 
 
System.out.println( Go to the static page after executing this page );return; 
% >  

Solution of JSP Technology to Realize Dynamic Page to Static Page 3:

Update problem

The following discussion is how to update and generate static files under 1. In fact, you can see from the above implementation that it is very simple to delete the generated static files. As for when to delete, it depends on your needs. I can think of several situations as follows:

When data updates are used to generate pages

If you don't need to provide timely data, you can update it regularly

Never update

Then through this JSP technology to achieve a dynamic page to static page, the transition from dynamic page to static has already come to a paragraph, do you have some inspiration? Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: