servlet jsp:include 與 include 指令的不同之處

jsp:include 和 include 的作用 就是 爲代碼重用 減少重複代碼

但是這個兩者有什麼不同了?  根本點 我們看看 jsp文件 轉換爲.java文件之後的源代碼 

*********************************jsp:include   所生成的java源文件***********************

out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write('\r');
      out.write('\n');
      org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "header.jsp", out, false);
      out.write('\r');
      out.write('\n');
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");

******************************************************************************


*******************************include的指令生成的源文件*****************

out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("<h1>This is public header</h1>\r\n");
      out.write("</body>\r\n");
      out.write("</html>");

      out.write('\r');
      out.write('\n');
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");

******************************************************************************

區別很明顯

include 指令 是先將包含的jsp文件內容複製過來  ,然後再一起轉換爲.java文件

而jsp:include標準動作 則是在原來的文件轉換爲.java文件之後, 通過  org.apache.jasper.runtime.JspRuntimeLibrary.include 去請求那個文件,然後再將響應插入進來。


優缺點:

jsp:include 標準動作 優點:總是能獲取引入文件的最新內容,因爲每次請求都會去讀取那個文件。

缺點:有性能消耗

include指令 優點:只會在轉換爲.java文件時,做一次讀取引入文件,性能消耗較少;缺點:不能在引入文件修改後,顯示最新的內容。(tomcat5+的可以實現自動辨識jsp文件是否修改過,但其他的服務器不一定可以哦)


發佈了61 篇原創文章 · 獲贊 6 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章