The method of implementing dynamic page to static page by JSP technology _jsp programming

Source: Internet
Author: User

This article is introduced the JSP technology realizes the dynamic page to the static page method, shares to everybody, concretely as follows:

For JSP technology to implement dynamic page to static page of the program, we have three steps to explain:

JSP technology to achieve dynamic page to static page of the project first:

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 
(﹤!--file start--﹥); 
%﹥ 
﹤html﹥ 
﹤head﹥ 
﹤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 Boole 
 
 an _jspx_inited = false; Public final void _jspx_init () throws org.apache.jasper.runtime.JspException {} public void _jspservice (HTTPSERVL Etrequest request, HttpServletResponse response) throws Java.io.IOException, servletexception {jspfactory _jspxf 
 Actory = 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 the Out.write (﹤!--file--﹥) that was generated after the ﹤%@ page Language=java contenttype=text/html;charset=gb2312%﹥; 
 
        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); 
 
      The 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.

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 (); 
 }

Believe that you may already know what to do here. 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.

JSP technology to achieve dynamic page to static page of the program second:

Implement a replacement

Assume

﹤%@ 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/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 (﹤!--documents started--﹥); 
%﹥ 
﹤html﹥ 
﹤head﹥ 
﹤body﹥ 
﹤%= See, this is the output is redirected to the implementation of the file, it is very simple 
^_^ 
%﹥ ﹤/body﹥ ﹤/head﹥ Html﹥ 
﹤% 
out.close (); 
 
Closes the generated static file Out_bak.clear ();p Agecontext.forward (filePath); 
 
SYSTEM.OUT.PRINTLN (execute this page and then go to the static page); 
%﹥ 

JSP technology to achieve dynamic page to static page of the program third:

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

So through this JSP technology to achieve dynamic page to static page of the scheme, from the dynamic page to the static transition has come to an ending, do you have a little inspiration? Thank you for reading, I hope to help you, thank you for your support for this site!

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.