JSP technology to achieve dynamic pages to static pages, jsp static pages

Source: Internet
Author: User

JSP technology to achieve dynamic pages to static pages, jsp static pages

This article describes how to implement dynamic pages to static pages by Using jsp technology. The details are as follows:

For the JSP technology to implement dynamic pages to static pages, we will explain from three steps:

Solution 1 of JSP technology implementing dynamic pages to static pages:

To get a deeper understanding of the origins of this framework, let's first take a look at the content of the JAVA file that the JSP parser converts the JSP code we wrote.

Below is a JSP file test. jsp

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

The content of the Java file test $ jsp. java converted by 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 reque St, 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. getdefafactory Factory (); 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 comment out added by the interpreter. write (\ r \ n ); // The previous sentence is caused by the out. write (<%@ page language = java contentType = text/html; charset = GB2312 %> generated by the line feed following it (<! -- File start -->); out. write (\ r \ n 

From the code above, we can clearly see how several built-in JSP objects (out, request, response, session, pageContext, application, config, page) are generated, A friend who understands servlet can understand it at a glance.

The following describes the out object, which is declared as the JspWriter type. JspWriter is an abstract class and can be found in the javax. servlet. jsp package.

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 that you may already know how to do this. Yes. If you want to steal a day, inherit the JspWriter class, implement its defined virtual function, and replace the out variable with your own instance of the class.

Solution 2 of JSP technology implementing dynamic pages to static pages:

Implement replacement

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 based on parameters _ + arg1 +. html; // first, determine whether the File already exists. If it does not exist, execute this page. Otherwise, the page jumps to the static page and the File f = new File (pageContext. getServletContext (). getRealPath (filePath); if (f. exists () {out_bak.clear (); pageContext. forward (filePa Th); System. out. println (directly go to the static page); return;} out = new HtmlIntoFile (pageContext. getServletContext (). getRealPath (filePath); out. write (<! -- File start -->); %> 

JSP technology to achieve dynamic pages to static pages solution 3:

Update question

Next we will discuss how to update and generate static files. As you can see from the above implementation, it is very easy to delete the generated static files, it depends on your needs. I can think of the following situations:

◆ When used to generate page data updates

◆ If you do not need to provide timely data, you can update the data on a regular basis.

◆ Never update

So the JSP technology is used to implement the dynamic page to Static Page solution. The transformation from dynamic page to Static Page has come to an end. Are you somewhat inspired? Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.