JSP technology implements dynamic pages to static pages

Source: Internet
Author: User

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

 
 
  1. <%@ PageLanguage=JavaContentType=Text/Html;Charset=GB2312%>
  2.  
  3. <%
  4. Out. write (<! -- File start --> );
  5. %>
  6.  
  7. <Html>
  8. <Head>
  9. <Body>
  10. <%= Output %>
  11. </Body>
  12. </Head>
  13. </Html>
  14.  

The content of the Java file test $ jsp. java converted by Tomcat is as follows:

 
 
  1. Package org. apache. jsp;
  2. Import javax. servlet .*;
  3. Import javax. servlet. http .*;
  4. Import javax. servlet. jsp .*;
  5. Import org. apache. jasper. runtime .*;
  6.  
  7. Public class test $ jsp extends HttpJspBase {
  8.  
  9. Static {
  10.  
  11. }
  12. Public testOutRedir $ jsp (){
  13. }
  14.  
  15. Private static boolean_ Jspx_inited=False;
  16.  
  17. Public final void _ jspx_init () throws org. apache. jasper. runtime. JspException {
  18.  
  19. }
  20. Public void _ jspService (HttpServletRequest request, HttpServletResponse response)
  21.  
  22. Throws java. io. IOException, ServletException {
  23. JspFactory_ JspxFactory=Null;
  24. PageContextPageContext=Null;
  25. HttpSessionSession=Null;
  26. ServletContextApplication=Null;
  27. ServletConfigConfig=Null;
  28. JspWriterOut=Null;
  29. ObjectPage=This;
  30. String_ Value=Null;
  31. Try {
  32.  
  33. If (_ Jspx_inited= False ){
  34. Synchronized (this ){
  35. If (_ Jspx_inited= False ){
  36. _ Jspx_init ();
  37. _ Jspx_inited=True;
  38.  
  39. }
  40. }
  41. }
  42. _ JspxFactory=JspFactory. Getdefafactory Factory ();
  43.  
  44. Response. setContentType (text/html;Charset=GB2312);
  45.  
  46. PageContext=_ JspxFactory. GetPageContext (this, request, response,
  47.  
  48. , True, 8192, true );
  49.  
  50. Application=PageContext. GetServletContext ();
  51. Config=PageContext. GetServletConfig ();
  52. Session=PageContext. GetSession ();
  53. Out=PageContext. GetOut ();
  54.  
  55. // To save space, I deleted the comments added by the interpreter.
  56.  
  57. Out. write (\ r \ n );
  58.  
  59. // The previous sentence is due
  60. <%@ PageLanguage=JavaContentType=Text/Html;Charset=GB2312%>
  61.  
  62. Out. write (<! -- File start --> );
  63.  
  64. Out. write (\ r \ n
  65.  
  66. Out. print (output );
  67.  
  68. Out. write (\ r \ n </body> \ r \ n
  69.  
  70. } Catch (Throwable t ){
  71.  
  72. If (out! = Null & out. getBufferSize ()! = 0)
  73.  
  74. Out. clearBuffer ();
  75.  
  76. If (pageContext! = Null) pageContext. handlePageException (t );
  77.  
  78. } Finally {
  79.  
  80. If (_ jspxFactory! = Null) _ jspxFactory. releasePageContext (pageContext );
  81.  
  82. }
  83.  
  84. }
  85.  
  86. }
  87.  

From the code above, we can clearly see how the out, request, response, session, pageContext, application, config, and page objects built in JSP 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.

 
 
  1. abstractpublicclassjavax.servlet.jsp.JspWriterextends java.io.Writer{  
  2.  
  3.       final public static intNO_BUFFER = 0;  
  4.  
  5.       final public static intDEFAULT_BUFFER = -1;  
  6.  
  7.       final public static intUNBOUNDED_BUFFER = -2;  
  8.  
  9.       protected intbufferSize;  
  10.  
  11.       protected BooleanautoFlush;  
  12.  
  13.       protectedjavax.servlet.jsp.JspWriter(intarg1,booleanarg2);  
  14.  
  15.         
  16.  
  17.    abstractpublicvoidnewLine()throwsIOException;  
  18.  
  19.    abstractpublicvoidprint(booleanarg0)throwsIOException;  
  20.  
  21.    abstractpublicvoidprint(chararg0)throwsIOException;  
  22.  
  23.    abstractpublicvoidprint(intarg0)throwsIOException;  
  24.  
  25.    abstractpublicvoidprint(longarg0)throwsIOException;  
  26.  
  27.    abstractpublicvoidprint(floatarg0)throwsIOException;  
  28.  
  29.    abstractpublicvoidprint(doublearg0)throwsIOException;  
  30.  
  31.    abstractpublicvoidprint(char[]arg0)throwsIOException;  
  32.  
  33.    abstractpublicvoidprint(Stringarg0)throwsIOException;  
  34.  
  35.    abstractpublicvoidprint(Objectarg0)throwsIOException;  
  36.  
  37.    abstractpublicvoidprintln()throwsIOException;  
  38.  
  39.    abstractpublicvoidprintln(booleanarg0)throwsIOException;  
  40.  
  41.    abstractpublicvoidprintln(chararg0)throwsIOException;  
  42.  
  43.    abstractpublicvoidprintln(intarg0)throwsIOException;  
  44.  
  45.    abstractpublicvoidprintln(longarg0)throwsIOException;  
  46.  
  47.    abstractpublicvoidprintln(floatarg0)throwsIOException;  
  48.  
  49.    abstractpublicvoidprintln(doublearg0)throwsIOException;  
  50.  
  51.    abstractpublicvoidprintln(char[]arg0)throwsIOException;  
  52.  
  53.    abstractpublicvoidprintln(Stringarg0)throwsIOException;  
  54.  
  55.    abtractpublicvoidprintln(Objectarg0)throwsIOException;  
  56.  
  57.    abstractpublicvoidclear()throwsIOException;  
  58.  
  59.    abstractpublicvoidclearBuffer()throwsIOException;  
  60.  
  61.    abstractpublicvoidflush()throwsIOException;  
  62.  
  63.    abstractpublicvoidclose()throwsIOException;  
  64.  
  65.    publicintgetBufferSize() ;  
  66.  
  67.    abstractpublicintgetRemaining();  
  68.  
  69.    publicbooleanisAutoFlush();  
  70.  
  71. }  
  72.  

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

 
 
  1. <%@ PageLanguage=JavaContentType=Text/Html;Charset=GB2312 Import=Jwb. Util. HtmlIntoFile, jwb. util. TempSinglet, java. io. File %>
  2. <%
  3.  
  4. JspWriterOutOut_bak= Out; StringArg1=Argument1; StringFilePath=/Cache/generate file name based on parameters _ + arg1 +. html;
  5.  
  6. // First determine whether the File already exists. If it does not exist, execute this page. Otherwise, the File will be OK when you jump to the static page.F=NewFile (pageContext. getServletContext (). getRealPath (filePath ));
  7.  
  8. If (f. exists () {out_bak.clear (); pageContext. forward (filePath); System. out. println (directly go to the static page );
  9.  
  10. Return ;}Out=NewHtmlIntoFile (pageContext. getServletContext (). getRealPath (filePath); out. write (<! -- File start --> );
  11. %>
  12. <Html>
  13. <Head>
  14. <Body>
  15. <%= Look, this is the implementation of redirecting the output to a file. It's easy ^ _ ^ %>
  16. </Body>
  17. </Head>
  18. </Html>
  19. <%
  20. Out. close ();
  21.  
  22. // Close the generated static file out_bak.clear (); pageContext. forward (filePath );
  23.  
  24. System. out. println (execute this page and then go to the static page); return;
  25. %>
  26.  

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?

  1. Analysis of Script Programming in JSP development
  2. JSP tutorial basics: Technical Features of JSP
  3. JSP2.0 features of basic JSP tutorial knowledge
  4. JSP entry-level website environment setup steps
  5. Introduction to JSP: several solutions to avoid repeated submission of Form forms

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.