和的区别

:动态包含

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

DynamicInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>动态包含title>  
  5.          head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <jsp:include page="header.html"flush="true"/>  
  9.    
  10.                    <tablebordertableborder="1" align="center">  
  11.                             <tr>  
  12.                                      <td>姓名td><td>性别td><td>年龄td><td>爱好td>  
  13.                             tr>  
  14.                             <tr>  
  15.                                      <td>atd><td>btd><td>ctd><td>dtd>  
  16.                             tr>  
  17.                    table>  
  18.          body>  
  19. html>  


Header.html :

[html] view plain copy
  1. <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  2.          动态包含的标题(HTML)  
  3. h2>  

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

[java] view plain copy
  1. out.write("\r\n");  
  2. out.write("\r\n");  
  3. out.write("\t\r\n");  
  4. out.write("\t\t动态包含\r\n");  
  5. out.write("\t\r\n");  
  6. out.write("\t\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. "color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);  
  10. out.write("\r\n");  
  11. out.write("\r\n");  
  12. out.write("\t\t\r\n");  
  13. out.write("\t\t\t\r\n");  
  14. out.write("\t\t\t\t姓名性别年龄爱好\r\n");  
  15. out.write("\t\t\t\r\n");  
  16. out.write("\t\t\t\r\n");  
  17. out.write("\t\t\t\tabcd\r\n");  
  18. out.write("\t\t\t\r\n");  
  19. out.write("\t\t\r\n");  
  20. out.write("\t\r\n");  
  21. out.write("");  

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

DynamicInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>动态包含title>  
  5.          head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <jsp:include page="header.jsp"flush="true"/>  
  9.    
  10.                    <tablebordertableborder="1" align="center">  
  11.                             <tr>  
  12.                                      <td>姓名td><td>性别td><td>年龄td><td>爱好td>  
  13.                             tr>  
  14.                             <tr>  
  15.                                      <td>atd><td>btd><td>ctd><td>dtd>  
  16.                             tr>  
  17.                    table>  
  18.          body>  
  19. html>  

Header.jsp :
[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <body>  
  4.                    <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  5.                             动态包含的标题(JSP)  
  6.                    h2>  
  7.          body>  
  8. html>  

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


DynamicInclude_jsp.java:

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>动态包含title>\r\n");  
  5. out.write("\thead>\r\n");  
  6. out.write("\t<bodystylebodystyle=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);span>  
  10. out.write("\r\n");  
  11. out.write("\r\n");  
  12. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  13. out.write("\t\t\t<tr>\r\n");  
  14. out.write("\t\t\t\t<td>姓名td><td>性别td><td>年龄td><td>爱好td>\r\n");  
  15. out.write("\t\t\ttr>\r\n");  
  16. out.write("\t\t\t<tr>\r\n");  
  17. out.write("\t\t\t\t<td>atd><td>btd><td>ctd><td>dtd>\r\n");  
  18. out.write("\t\t\ttr>\r\n");  
  19. out.write("\t\ttable>\r\n");  
  20. out.write("\tbody>\r\n");  
  21. out.write("html>");  


header_jsp.java:    

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<body>\r\n");  
  4. out.write("\t\t<h2 style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");  
  5. out.write("\t\t\t动态包含的标题(JSP)\r\n");  
  6. out.write("\t\th2>\r\n");  
  7. out.write("\tbody>\r\n");  
  8. out.write("html>");  


<%@include%>:静态包含

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

StaticInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>静态包含title>  
  5.          head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <%@include file="header.jsp"%>  
  9.                    <tablebordertableborder="1" align="center">  
  10.                             <tr>  
  11.                                      <td>姓名td><td>性别td><td>年龄td><td>爱好td>  
  12.                             tr>  
  13.                             <tr>  
  14.                                      <td>atd><td>btd><td>ctd><td>dtd>  
  15.                             tr>  
  16.                    table>  
  17.          body>  
  18. html>  

header.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  3.          静态包含的标题(JSP)  
  4. h2>  

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

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>静态包含title>\r\n");  
  5. out.write("\thead>\r\n");  
  6. out.write("\t<body style=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. out.write("\r\n");  
  10. <span style="color:#ff0000;">out.write("<h2styleh2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");  
  11. out.write("\t静态包含的标题(JSP)\r\n");  
  12. out.write("h2>");span>  
  13. out.write("\r\n");  
  14. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  15. out.write("\t\t\t<tr>\r\n");  
  16. out.write("\t\t\t\t<td>姓名td><td>性别td><td>年龄td><td>爱好td>\r\n");  
  17. out.write("\t\t\ttr>\r\n");  
  18.  out.write("\t\t\t<tr>\r\n");  
  19. out.write("\t\t\t\t<td>atd><td>btd><td>ctd><td>dtd>\r\n");  
  20. out.write("\t\t\ttr>\r\n");  
  21. out.write("\t\ttable>\r\n");  
  22. out.write("\tbody>\r\n");  
  23. out.write("html>");  

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

StaticInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>静态包含title>  
  5.          head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <%@include file="header.html"%>  
  9.                    <tablebordertableborder="1" align="center">  
  10.                             <tr>  
  11.                                      <td>姓名td><td>性别td><td>年龄td><td>爱好td>  
  12.                             tr>  
  13.                             <tr>  
  14.                                      <td>atd><td>btd><td>ctd><td>dtd>  
  15.                             tr>  
  16.                    table>  
  17.          body>  
  18. html>  

header.html:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  3.          静态包含的标题(HTML)  
  4. h2>  


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

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>静态包含title>\r\n");  
  5. out.write("\thead>\r\n");  
  6. out.write("\t<bodystylebodystyle=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. out.write("\r\n");  
  10. <span style="color:#ff0000;">out.write("<h2styleh2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");  
  11. out.write("\t静态包含的标题(HTML)\r\n");  
  12. out.write("h2>");span>  
  13. out.write("\r\n");  
  14. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  15. out.write("\t\t\t<tr>\r\n");  
  16. out.write("\t\t\t\t<td>姓名td><td>性别td><td>年龄td><td>爱好td>\r\n");  
  17. out.write("\t\t\ttr>\r\n");  
  18. out.write("\t\t\t<tr>\r\n");  
  19. out.write("\t\t\t\t<td>atd><td>btd><td>ctd><td>dtd>\r\n");  
  20. out.write("\t\t\ttr>\r\n");  
  21. out.write("\t\ttable>\r\n");  
  22. out.write("\tbody>\r\n");  
  23. out.write("html>");  

由上可以总结出:

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

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

你可能感兴趣的:(Java,Web)