This article introduces the method of implementing dynamic pages to static pages in jsp technology. I will share it with you. The details are as follows:
For JSP technology to implement dynamic pages to static pages, we will explain from three steps:
The first solution for JSP technology to implement dynamic pages to static pages:
In order to understand the origin of this framework in a simple way, let’s first understand the content of the JSP parser converts the JSP code we wrote into JAVA file.
Below is a JSP file
﹤%@ page language=java contentType=text/html;charset=GB2312 %﹥ ﹤% (﹤!--File start--﹥); %﹥ ﹤html﹥ ﹤head﹥ ﹤body﹥ ﹤%=Output%﹥ ﹤/body﹥ ﹤/head﹥ ﹤/html﹥
The content of the Java file test$ converted through Tomcat is as follows:
package ; import .*; import .*; import .*; import .*; public class test$jsp extends HttpJspBase { static { } public testOutRedir$jsp( ) { } private static boolean _jspx_inited = false; public final void _jspx_init() throws { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws , 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 = (); (text/html;charset=GB2312); pageContext = _jspxFactory.getPageContext(this, request, response, , true, 8192, true); application = (); config = (); session = (); out= (); //To save space, I deleted the comments added by the interpreter (\r\n); //The previous sentence is because ﹤%@ page language=java contentType=text/html;charset=GB2312 %﹥The following line breaks are generated (﹤!--File start--﹥); (\r\n﹤html﹥\r\n﹤head﹥\r\n﹤body﹥\r\n); (Output); (\r\n﹤/body﹥\r\n﹤/head﹥\r\n﹤/html﹥\r\n); } catch (Throwable t) { if (out!= null &&() != 0) (); if (pageContext != null) (t); } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext); } } }
From the above code, you can clearly see how several built-in objects (out, request, response, session, pageContext, application, config, page) are generated by JSP. Friends who understand servlets can understand it at a glance.
Let’s focus on understanding the out object, which is declared as the JspWriter type. JspWriter is an abstract class, and its definition can be found in the package.
{ final public static intNO_BUFFER = 0; final public static intDEFAULT_BUFFER = -1; final public static intUNBOUNDED_BUFFER = -2; protected intbufferSize; protected BooleanautoFlush; (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 at this point you may already know how to do it. Yes, it's OK to have a day to change the day, inherit the JspWriter class, then implement the virtual function it defines, and then replace the out variable with an instance of the class you implemented by yourself.
JSP technology implements the second solution of dynamic pages to static pages:
Implement replacement
Assumptions
﹤%@ page language=java contentType=text/html;charset=GB2312 import=,,%﹥ ﹤% JspWriter outout_bak =out;String arg1=argument1;String filePath = /cache/Generate file names based on parameters_ + arg1 + .html; //First determine whether the file already exists. If it does not exist, execute this page. Otherwise, it will be OK to jump to the static page. File f = new File(().getRealPath(filePath)); if(()){ out_bak.clear(); (filePath); (Go directly to the static page); return;}out= new HtmlIntoFile(().getRealPath(filePath));(﹤!--File start--﹥); %﹥ ﹤html﹥ ﹤head﹥ ﹤body﹥ ﹤%= See,This is the implementation of the output being redirected to the file,Very simple^_^%﹥ ﹤/body﹥ ﹤/head﹥ ﹤/html﹥ ﹤% (); //Close the generated static file out_bak.clear();(filePath); (After executing this page, go to the static page);return; %﹥
The third solution of JSP technology to implement dynamic pages to static pages:
Update question
Let’s discuss how to update and generate static files. In fact, from the above implementation, you can see that it is very simple to delete the generated static files. As for when to delete it, it depends on your needs. Several situations I can think of are as follows:
◆When data used to generate page updates
◆If you do not need to provide timely data, you can update it regularly
◆Never update
So through this JSP technology, the solution of dynamic pages to static pages has come to an end. Are you a little inspired? Thank you for reading, I hope it can help you. Thank you for your support for this site!