和的区别

前天上了jsp的课,老师讲了一下<%@include%>和的区别,由于演示的太快,我没有看太清,所以回来之后,自己又写了个例子,好好看看,还是发现了一些东西的。

:动态包含

第一种情况(包含的是html文件):

DynamicInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%>

         
                   动态包含
         
         
 
                   
 
                   
                            
                                     姓名性别年龄爱好
                            
                            
                                     abcd
                            
                   
         


Header.html :


         动态包含的标题(HTML)

运行之后,只生成一个servlet,和上面的代码对应如下:

     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\r\n");
     out.write("\t\t动态包含\r\n");
     out.write("\t\r\n");
     out.write("\t\r\n");
     out.write("\r\n");
     out.write("\t\t");
     org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);
     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t
姓名性别年龄爱好
abcd
\r\n"); out.write("\t\r\n"); out.write("");

第二种情况(包含的是jsp文件):

DynamicInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%>

         
                   动态包含
         
         
 
                   
 
                   
                            
                                     姓名性别年龄爱好
                            
                            
                                     abcd
                            
                   
         

Header.jsp :

<%@pagecontentType="text/html;charset=gb2312"%>

         
                   
                            动态包含的标题(JSP)
                   
         

运行之后,生成了两个servlet:DynamicInclude_jsp.java和header_jsp.java,这也是为什么 Header.jsp中要写上<%@page contentType="text/html;charset=gb2312"%>和完整的和,而Header.html不用写的原因。因为前者两个.jsp文件是两个相互独立的整体,它们之间的关系是通过request和reponse来发生的,而后者只是简单的嵌套。两个servlet对应的代码如下:


DynamicInclude_jsp.java:

     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\r\n");
     out.write("\t\t动态包含\r\n");
     out.write("\t\r\n");
     out.write("\t\r\n");
     out.write("\r\n");
     out.write("\t\t");
     org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);
     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t
姓名性别年龄爱好
abcd
\r\n"); out.write("\t\r\n"); out.write("");


header_jsp.java:    

     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\r\n");
     out.write("\t\t

\r\n"); out.write("\t\t\t动态包含的标题(JSP)\r\n"); out.write("\t\t

\r\n"); out.write("\t\r\n"); out.write("");


<%@include%>:静态包含

第一种情况:<%@include%>包含的是jsp文件。

StaticInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%>

         
                   静态包含
         
         
 
                   <%@include file="header.jsp"%>
                   
                            
                                     姓名性别年龄爱好
                            
                            
                                     abcd
                            
                   
         

header.jsp:

<%@pagecontentType="text/html;charset=gb2312"%>

         静态包含的标题(JSP)

运行之后,只生成一个servlet,和上面的代码对应如下:

     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\r\n");
     out.write("\t\t静态包含\r\n");
     out.write("\t\r\n");
     out.write("\t\r\n");
     out.write("\r\n");
     out.write("\t\t");
     out.write("\r\n");
     out.write("\r\n");
     out.write("\t静态包含的标题(JSP)\r\n");
     out.write("");
     out.write("\r\n");
     out.write("\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
      out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t
姓名性别年龄爱好
abcd
\r\n"); out.write("\t\r\n"); out.write("");

第二种情况:<%@include%>包含的是html文件。

StaticInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%>

         
                   静态包含
         
         
 
                   <%@include file="header.html"%>
                   
                            
                                     姓名性别年龄爱好
                            
                            
                                     abcd
                            
                   
         

header.html:

<%@pagecontentType="text/html;charset=gb2312"%>

         静态包含的标题(HTML)


运行之后,也是只生成一个servlet,和上面的代码对应如下:

     out.write("\r\n");
     out.write("\r\n");
     out.write("\t\r\n");
     out.write("\t\t静态包含\r\n");
     out.write("\t\r\n");
     out.write("\t\r\n");
     out.write("\r\n");
     out.write("\t\t");
     out.write("\r\n");
     out.write("\r\n");
     out.write("\t静态包含的标题(HTML)\r\n");
     out.write("");
     out.write("\r\n");
     out.write("\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t\t\t\r\n");
     out.write("\t\t\t\r\n");
     out.write("\t\t
姓名性别年龄爱好
abcd
\r\n"); out.write("\t\r\n"); out.write("");

由上可以总结出:

对于静态包含,<%@include%>,中包含的文件,只是简单的嵌入到主文件中,就是在jsp页面转化成Servlet时才嵌入到主文件中,因为运行的结果是只生成了一个Servlet。

而对于动态包含,如果被包含文件是动态的,那么就会生成两个Servlet,也就是被包含文件也要经过jsp引擎编译执行生成一个Servlet,两个Servlet通过request和reponse进行通信。如果被包含的文件是静态的,那么这种情况和<%@include>就很相似,只生成了一个Servlet,但是他们之间没有进行简单的嵌入,而依然是通过request和reponse进行的通信。



你可能感兴趣的:(J2EE)